Skip to main content
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 ...
Ciro Santilli OurBigBook.com's user avatar
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) ...
MARS's user avatar
  • 5,169
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          ...
DinoCoderSaurus's user avatar
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) ...
chad's user avatar
  • 239
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 ...
Cliff B's user avatar
  • 69k
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(). ...
Cliff B's user avatar
  • 69k
3 votes
Accepted

Why can you say the name of an array is a pointer to the first element of the array in C?

First I commend you for asking the question and trying to affirm your understanding of the language and what you are learning. It is admirable and requires confidence and a degree of bravery. And it ...
UpAndAdam's user avatar
  • 1,426
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 ...
curiouskiwi's user avatar
  • 18.7k
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 ...
Yuri Laguardia's user avatar
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.
DinoCoderSaurus's user avatar
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 ...
kzidane's user avatar
  • 17.7k
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 ...
Cliff B's user avatar
  • 69k
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 ...
Cliff B's user avatar
  • 69k
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 ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

check50 claiming pairs are sorted incorrectly

I suspect this "introduced another global array called strength_of_victory" is the problem. Recall from the spec: You should not modify anything else in tideman.c other than the implementations ...
DinoCoderSaurus's user avatar
1 vote
Accepted

In pset2, Vigenere, I made and used an array that I would like for it to be explained

The detail you seem to miss is that a char array is not a string unless it ends in the NUL char '\0' If you have char text[] = "Foobar";, that is stored {'F', 'o', 'o', 'b', 'a', 'r', '\0'} ...
curiouskiwi's user avatar
  • 18.7k
1 vote
Accepted

Pset2 Vigenere, array with garbage

Did you remember to store the end of string marker in cipher? Did you allow space to store it? If this answers your question, please click on the check mark to accept. Let's keep up on forum ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Caesar not getting output

for (i = 0, c = strlen(p); i < c; i++) { if isupper(c) Although c is defined as a char, it is being used as an int, the length of the string, not a char. You need to pass a char from the ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Why string variable has no square bracket?

The reason why a string variable does not have brackets, is because it is not a string array. (It is a char array or a char pointer.) In fact, string is a new type, defined in the cs50 library. It ...
Peter Pesch's user avatar
  • 1,009
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 ...
Blauelf's user avatar
  • 21k
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 ...
akshayk.vnit's user avatar
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 ...
aquablitz11's user avatar
1 vote

Excess elements in scalar initilizer?

By doing int example[]; you are initializing a pointer to an array whose size is not defined. After that in int example = {1,2,3}; yu are adding values to the pointer which is illogical and has no ...
Kapil Bhatt's user avatar
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 ...
Blauelf's user avatar
  • 21k
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 ...
Blauelf's user avatar
  • 21k
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.
DinoCoderSaurus's user avatar
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.
DinoCoderSaurus's user avatar
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);...
DinoCoderSaurus's user avatar
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,
obesman's user avatar
  • 1,139
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 ...
ChrisG's user avatar
  • 7,396

Only top scored, non community-wiki answers of a minimum length are eligible