8
votes
Difference between char and char* in c
C99 N1256 draft
There are two different uses of character string literals:
Initialize char[]:
char c[] = "abc";
This is "more magic", and described at 6.7.8/14 "Initialization":
An array ...
4
votes
Accepted
pset2 caesar converting from ascii to alphabetical
Your formula is correct as well as the understanding of the formula, only that you are mixing two things in a wrong way, look carefully at the following expression:
((('s[i]' - 97) + n) % 26) + 97)
...
4
votes
Accepted
Pset4 Resize.c Array Overstepping
After edit:
For reference, here is the pseudocode from the walkthrough:
for each row
for each pixel
...
3
votes
Accepted
Pset4 - Recover (usage of sprintf)
Before I answer this question and important clarification must be made about what it means for something to "work". There is a term that will come up a lot when talking about C (and other languages) ...
3
votes
Accepted
trail of strange characters after printing character array
Uhhuh. I see that it is doing a nice job of encoding and storing the encoded characters in an array. But have you also stored the end of string marker '\0' at the end of the string?
printf will ...
3
votes
Accepted
Week 2 passing array example problem?
When the code calls set_int(a), the value stored in x in main(), 10, is passed to set_int(). That "10" is a copy of the original and is stored in a local variable, x, inside of set_int(). ...
2
votes
Accepted
declaring char array vs malloc
yes, indeed there is a difference! when you use malloc, the block of memory that you are trying to allocate gets allocated in a region of memory called the heap. freeing memory allocated on the heap ...
2
votes
Accepted
c prog - Generate ''ALPHABET'' and ''alphabet'' in arrays, wrong output
The problem is simple. You have created two string arrays. Each holds the 26 letter alphabet, one lower case, the other upper case. They are created in sequential memory. In this case, the UC array ...
2
votes
Accepted
how effective would it have been to use an array for the dictionary?
suppose we are to implement speller using an array instead of a hash-table or a trie.
a main problem that we have is that we don't know the number of words in the dictionary in advance (yes, we know ...
2
votes
Accepted
How to use fseek along side arrays? Pset4 recover
for(int i = 0; i < MEMORYBLOCK; i++)
{
//read & store 512bytes in memory[i]
fread(&(store[i]), 1, 1, inptr); //do we need *inptr instead?
}
If you want to ...
2
votes
Accepted
Split a sentence ito an array of words without using strtok
I'll try to give you a visualization of what anfauglit explained.
This is what your program is doing:
https://goo.gl/4MfFx4 (click the link, wait some seconds for it to load and then keep clicking ...
2
votes
Accepted
Problem accessing the last element of the array
If an array contains 27 elements and the first element is index 0, then the last element is index 26. The last index is always one less than the number of elements, because indexes start at 0.
2
votes
Accepted
array size in C
In C, when you declare an array, it's size MUST be declared when it is created. There is no mechanism built into C for dynamic array RE-sizing. An array's size is declared explicitly by a statement ...
2
votes
Accepted
Caesar : Extra random characters at the end of a char ciphertext array
You have to understand how printf and strlen work. Both of them (and several other functions) depend on the existence of the end of string marker, \0 at the end of the target string. If the EOS marker ...
1
vote
Accepted
Pset2: Copying string contents
You declare ky as string[keylen], it should be char[keylen+1] (don't forget the null terminator, that's why +1).
Don't forget to check argc before accessing argv[1], it might not exist at all.
Don't ...
1
vote
Accepted
Declaring an array of structure pointers
It is the same as everywhere else. If your struct is for example struct node, an array of pointers would be for example struct node *myarray[ARR_LENGTH] (assuming ARR_LENGTH to be a constant meaning ...
1
vote
Accepted
Array declaration need explicit size?
C requires you to declare the size of an array explicitly unless you initialize it within the declaration.
Example: int array[] ; might not always work. Some compilers might allow it, but it is ...
1
vote
Accepted
Pset4 - Array declaration- Heap
Arrays you declare as int array[42]; are stored on stack, like any other variable.
For arrays like int *array = (int*)malloc(42*sizeof(int));, a pointer array is stored on stack, the memory block of ...
1
vote
Accepted
Pset5 Dictionary C Struct Array Call In Load Function Issue
fscanf needs some memory to write to, so char word[LENGTH + 1]; is probably the way to go. You might want to strcpy the result to a new node's word property in case of success.
Please note that the ...
1
vote
Accepted
I'm getting a runtime error on an array and I'm not sure why:
reg is declared as an array with 15 elements. It has indexes 0 through 14. There is no reg[15]. Without seeing the rest of the code, one might assume it fails after r++ executes for the 15th time.
1
vote
Accepted
Passing SQL values into arrays
This categories = [amounts[0]["category"]] looks suspicious, since categories is always updated with the first element of amounts. Ditto total_amount.
1
vote
Accepted
pset vigenere skipped over if statement
It's not skipping an if. The printf function does run. However, it's "printing" an "unprintable" character.
Check your equations. When i,k = 3
(((s[i] - 97) + (toupper(argv[1][k]) - 65) % 26) + 97);...
1
vote
Accepted
pset2 vigenere shifting characters by the same
There is a nice function toupper, look what it does. Alternatively, you can check if a char in key is less than 91 and if not, subtract 32,
1
vote
Accepted
pset7: why do we need [0] in from of list? (e.g. someData[0]['dic']?)
It's because when you run an SQL query with execute(), all the affected records in the database are returned to you, in an array of dictionaries. So you want the first dictionary in that list. But if ...
1
vote
Accepted
Game of Fifteen - Move Function - getting wrong coordinates?
(new reply as I misread!)
You would expect when tile==3, that x,y is 1,2 :
0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,2
Remember, it's row,col so what you are seeing is correct.
(Your init is doing the ...
1
vote
How could you create a dynamic array of strings in C?[Beginner]
You can ask user to separate names with comma or space or some other delimiter. You can find strchr() function useful for parsing names in such an input.
Another option is to ask further names in a ...
1
vote
pset3 move: won't move tiles in the same row
The move() code seems to be working fine. In looking at your output (the raw form, not the version that Stack reformatted before I edited it to show as output), it looks to me that maybe you have a ...
1
vote
Accepted
Subscripted value is not an array
The variable a is declared as a single integer. In the line if (a > a[y+1]) this code attempts to compare a to an array of the same variable name. This presents two problems. First, two vars can't ...
1
vote
Difference between Pass by Value and Pass by Reference (a practical example)
For set_int() function, it's not complicated. You understand it correctly. However, things are confusing for C arrays. Actually, to me, accessing array member via bracket notation is just a syntactic ...
1
vote
Accepted
what is the funny symbol printed when char[] uninitialized
That's a non-alpha ASCII value that is attempting to be printed. If you really want to know what it is, try printing it as an integer. Something along the lines of printf("the char ascii value is %i\...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
array × 116pset2 × 21
c × 19
string × 14
pointers × 12
cs50x × 8
caesar × 8
pset3 × 7
pset4 × 6
pset1 × 5
credit × 5
week2 × 5
structs × 5
arrays × 5
pset5 × 4
recover × 4
pset7 × 4
memory × 4
command-line-arguments × 4
char × 4
multi-dimensional-arrays × 4
speller × 3
resize × 3
vigenere × 3
pset8 × 3