By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

Initializing structure member( char array)

P: 3
I have declared an char array in my structure.After creating instance of the structure,i initialized its members in following way.

structure student
{
char name[20];
int rol_no;
}

int main()
{
student s1;
s1.name = "jimmy";
// some more statements..
}

this code gives me compilation error as "lvalue required before '=' operator "

Can anyone please explain me why??
Sep 20 '06 #1
Share this Question
Share on Google+
2 Replies


P: 61
As soon as you declare the variable of the structure, it takes memory. So it will take the memory for the array s1.name and the base address of this array get stored in s1.name.

Now the statement

s1.name = "jimmy";

is trying to change the base address of the array which is not possible.

Instead you use the strcpy(s1.name,"jimmy").

Let's analyse the following code.

char s[10];
s="jimmy";

Here s is an array of 10 characters. So in 10 bytes of memory will be allocated for storing the characters and we know that the base address will be stored in s.

Let us suppose that the base adress of the array be 100 and so it is get stored in s.

But when you print the size of the array it shows that 10 bytes have been occupied by this array. Then where did the s get stored, i.e., the base address 100 get stored.

So it means that it does not take any space for storing the base address of the array and the statement

s="jimmy";

is trying to change the base address of the array.
Sep 20 '06 #2

P: 3
Thanks for ur reply..
Sep 20 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.