C Programming: error: assignment to expression with array type

With this one I have this error ( error: assignment to expression with array type ). So I decided to do it using pointers and it actually working this is the code I used:

#include #include typedef struct < char *nom; char *prenom; int note; >Etu; int main()

So the question is what is the difference between both of them since strings are pointers. thank you ..

143k 11 11 gold badges 129 129 silver badges 204 204 bronze badges asked May 15, 2020 at 20:22 Reda El Idrissi Reda El Idrissi 37 1 1 silver badge 5 5 bronze badges This has been probably answered before in SO. Commented May 15, 2020 at 20:28 Does this answer your question? C - Difference between "char var[]" and "char *var"? Commented May 15, 2020 at 20:30 "since strings are pointers". That's not a correct assumption. Commented May 15, 2020 at 20:31

3 Answers 3

Strings are not pointers, they are sequences of characters terminated with a null byte. These sequences are stored in arrays, which are not pointers either, but objects that live in memory and to which a pointer can point. A pointer is a variable that contains the address of an object in memory.

In the first example, you try and store a string into an array with = . C does not support this type of assignment and complains with an explicit error message. You can copy the string into the array with strcpy , assuming the destination array is large enough to store the bytes of the string, including the null terminator. "reda" uses 5 bytes, so it is OK to copy it into E[0].nom :

#include #include typedef struct < char nom[20]; char prenom[20]; int note; >Etu; int main()

The second example uses a different approach: nom is now defined as a pointer to char . Defining the array E in main leaves it uninitialized, so you can only read from the pointers after you set their value to point to actual arrays of char somewhere in memory. E[0].nom = "reda"; does just that: set the address of the string literal "reda" into the member nom of E[0] . The string literal will be placed by the compiler into an array of its own, including a null terminator, and that must not be changed by the program.

answered May 15, 2020 at 20:30 143k 11 11 gold badges 129 129 silver badges 204 204 bronze badges

Thank you a lot . i just have another question: if we use scanf("%s",E[0].nom) it does work properly even when we are not using nom as a pointer but just as an array of chars

Commented May 15, 2020 at 21:07 @RedaElIdrissi: to avoid buffer overruns, you should write scanf("%19s", E[0].nom) Commented May 15, 2020 at 21:34

@RedaElIdrissi: it works because when you pass an array to a function, the function receives a pointer to the first element of the array.

Commented May 15, 2020 at 21:39

First part, you try to copy two array of character (string is not a pointer, it is array of character that is terminated by null character \0 ).

If you want to copy value of an array to another, you can use memcpy , but for string, you can also use strcpy .

 E[0].nom = "reda"; 
strcpy(E[0].nom,"reda"); 

Second part, you make the pointer point to string literal. The pointer points to first character of the string ( r in this case).

answered May 15, 2020 at 20:28 3,669 2 2 gold badges 10 10 silver badges 31 31 bronze badges

It is a lower-level concept actually.

If we say char nom[20] than its memory location will be decided at compile time and it will use stack memory (see the difference between stack memory and heap memory to gain more grasp on lover level programming). so we need to use it with indexes.

nom[0] = 'a'; nom[1] = 'b'; // and so on. 

On the other hand if we create a string using the double quotes method (or the second way in which you used pointers). The double quoted strings are identified as const char* by the compiler and the string is placed at the heap memory rather than the stack memory. moreover their memory location is not decided at compile time.

a very efficient way of testing the difference between these two types of strings is that when you use sizeof() with char[20] nom it will return 20 * sizeof(char) which evaluates to 20 without any doubt. it clearly states that whether you use only 5 characters or 2 characters it will always occupy space of 20 charaters (in stack memory)

but in case of const char* if you use sizeof operator on this variable than it will return the size of the pointer variable which depends on the hardware (32bit will have 4bytes pointer) and (64bit will have 8bytes pointer). So, the sizeof operator do not shows how many characters are stored in the pointer. it just shows the size of pointer. but the benefit of using const char* is that only take memory that is required if 4 characters are stored than it will consume 5 byte memory (last byte is additionally added in case of strings) and in case of 8 characters it will consume 9 bytes and so on but note that the characters will be stored in heap(memory)

 pointer --------------------> "characters" (on stack) (heap) 

What you have been doing in the first problem is that you were assigning a string that is only stored on heap to a data types that is only stored on stack. it is not the matter of string. it the matter of memory model

I hope you might have understood. I not i can elaborate more in comments.

an additional information. Stack Memory is faster compared to the Heap Memory.