Where are the Strings in C? :confused:
Printable View
Where are the Strings in C? :confused:
Strings in C are just characters arrays!!!
to declare one it's just for example
char string[] = "thisisastring";
and it will create a character array containing "thisisastring" or
char string[10];
and it will create an array with 10 positions
I think you should read a tutorial ;)
That makes perfect sense. I'm just glancing over C and found it interesting they don't just use String. I guess using a char array does give you better control though. Thanks.
There is no string type in C. Furthermore, while it is true that they're arrays of characters, there is one omitted fact that must hold true for it to be considered a string:
All strings are null terminated. Without being null terminated, it is not a string, but rather its just an array of characters.
char array[3] = { 'a', 'b', 'c' };
This is not a string.
char array[4] = { 'a', 'b', 'c', '\0' };
This is a string.
Strings may be actual arrays, or may be allocated via pointers, such as:
char *s = "mystring";
In this case, because we're using the double quotes, the null is automaticly provided.
char *s;
s = malloc( 10 );
strncpy( s, "hello", 5 );
s[5] = '\0';
Here we've manually allocated some space, copied in some data to said space, and just for good measure, provided a null.
I could have just used 'strcpy' and had it automaticly provide the null for me. Or, I could have used 6 for the size instead of 5, and it would have copied the null there also with strncpy.
Quzah.
Strings in C are variable length character arrays terminated with the null character.
Ok, using this code, how can I have the actual string displayed instead of the memory address?
Code:#include <stdio.h>
void showStringValues()
{
char *string = "Some Text";
printf("%d", string);
}
int main()
{
showStringValues();
return 0;
}
Code:char something[] = "hello";
printf("%s", something);
/* more code */
Thanks!
So what's the difference between using %d and %s?
%s is for characters, while %d is for integers
Thanks. :)