Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert from char* to char[] in c

here is a code sample

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

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

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

how can i achieve this assignment?

like image 327
Bassel Shawi Avatar asked Jan 15 '10 19:01

Bassel Shawi


People also ask

What does char * [] mean in C?

char* means a pointer to a character. In C strings are an array of characters terminated by the null character.

Can you convert char * to string?

We can convert a char to a string object in java by using the Character. toString() method.

What is the difference between char [] and char *?

Difference between char s[] and char *s in CThe s[] is an array, but *s is a pointer. For an example, if two declarations are like char s[20], and char *s respectively, then by using sizeof() we will get 20, and 4. The first one will be 20 as it is showing that there are 20 bytes of data.

Can you convert char to string in C?

Given a string str and a character ch, this article tells about how to append this character ch to this string str at the end. Use the strncat() function to append the character ch at the end of str. strncat() is a predefined function used for string handling.


1 Answers

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

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

You can't assign arrays using the = operator.

like image 97
John Bode Avatar answered Oct 13 '22 13:10

John Bode