tags:

views:

290

answers:

3

Hi

I need to convert a string to char array in c? How can i do this

or atleast tell me how to extract a single char from string as incremental in next ???

Thanks in advance

+1  A: 

In C, there's no (real, distinct type of) strings. Every C "string" is an array of chars, zero terminated.

Therefore, to extract a character c at index i from string your_string, just use

char c = your_string[i];

Index is base 0 (first character is your_string[0], second is your_string[1]...).

alamar
are they not null terminated.
Omar Kooheji
'\0' (the null character) is defined to have integer value 0.
Artelius
It's called ASCII*Z* for a reason.
alamar
+4  A: 

Hi there,

In C, a string is actually stored as an array of characters, so the 'string pointer' is pointing to the first character. For instance,

char myString[] = "This is some text";

You can access any character as a simple char by using myString as an array, thus:

char myChar = myString[6];
printf("%c\n", myChar); // Prints s

Hope this helps! David

David Oakley
Wouldn't that print 's'?
alamar
Yes, yes it would. And standard C - as opposed to what?
Artelius
A: 

chupame bien la verga