What's wrong with my code.

WBahn

Joined Mar 31, 2012
25,760
It works. Thanks for your help.
But I'm still wondering why we can't assign strings like that in the function?
The basic problem is that when you created this global variable st, you created as an array of structures that is located at a specific place in memory that was allocated for it.

Let's say that each data structure of type 'student' takes 44 bytes of data. So when you declared st[3] the memory manager allocated 132 bytes of memory. Let's say that the base address of this block is at address 1000.

That means that st[0] starts at 1000, st[1] starts at 1044, and st[2] starts at 1088.

Within those, name is the 30 bytes at the very beginning, so

st[0].name is at address 1000.

This is a constant address. You can't change it. It needs to allows point to THAT memory location, otherwise it is not within the st[0] data structure.

The string "Patrick is a stupid guy" is a string literal that is, most likely, stored someplace as part of the code itself. Perhaps that string is located at address 5440. The string, like all such strings, evaluates to the address at which it is stored. So you statement tries to get st[0].name to point to address 5440, but that would result in it pointing to a location other than where it HAS to point -- so the compiler throws an error.
 
Top