views:

253

answers:

4

hello, here is a code sample

void()
{
   char c[100];
   scanf("%s",c);
   char c2[100]=c;
}

my problem is when i do this assignment an error says that i can assign

char * "c"  to char[] "c2";

how can i achieve this assignment?

+3  A: 

You need to use strcpy()

char c2[100];
strcpy(c2, c);
Fred Larson
+4  A: 

You'll have to use strcpy() (or similar):

...  
char c2[100];
strcpy(c2, c);

You can't assign arrays using the = operator.

John Bode
I wish I'd said that. ;v)
Fred Larson
+1  A: 

char [] is not a valid value type in C (its only a valid declaration type), so you can't actualy do anything with char [] types. All you can do is convert them to something else (usually char *) and do something with that.

So if you wany to actually do something with the data in the array, you need to use some function or operation that takes a char * and derefences it. Obvious choices for your example are strcpy or memcpy

Chris Dodd
+2  A: 

Better practice would be to use strncpy(c2, c, 100) to avoid buffer overflow, and of course limit the data entry too with something like scanf("%99s", c);

aiGuru
What you said is correct, but some general C advice for other readers:1. Be careful with `strncpy` since it won't necessarily NUL-terminate. (Not a problem here since both `c` and `c2` have the same number of elements, but OTOH, that also means plain `strcpy` wouldn't be a problem either.)2. `fgets` would be better than `scanf`. http://c-faq.com/stdio/scanfprobs.html
jamesdlin