It is not entirely clear what you are looking to do, but a string in C is actually a char *, terminated by the NUL character '\0'. In other words, a single char is a character–where a string is an array of char.
As an example, the following are equivalent definitions of strings.
char hello[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
char hello[] = "Hello";
Note that in hello[size], in this case size = 6, size needs to be at least the size of the string, including the null terminator '\0'.
As I said previously, it is not completely clear what you are trying to do–if you want to build an array of strings (not the question asked) then I will gladly help you in doing so.
Tutorials on using strings and string.h are vastly available on the web, but I suggest you look for a more comprehensive C course (Harvard's CS50 is a good place to start, and is free).
Good luck,
Alexandre.
Answer from Alexandre Cassagne on Stack OverflowIt is not entirely clear what you are looking to do, but a string in C is actually a char *, terminated by the NUL character '\0'. In other words, a single char is a character–where a string is an array of char.
As an example, the following are equivalent definitions of strings.
char hello[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
char hello[] = "Hello";
Note that in hello[size], in this case size = 6, size needs to be at least the size of the string, including the null terminator '\0'.
As I said previously, it is not completely clear what you are trying to do–if you want to build an array of strings (not the question asked) then I will gladly help you in doing so.
Tutorials on using strings and string.h are vastly available on the web, but I suggest you look for a more comprehensive C course (Harvard's CS50 is a good place to start, and is free).
Good luck,
Alexandre.
Here is a complete C program that declares a string and prints it:
#include<stdio.h>
int main() {
char name[] = "John Q Public"; //declare a string
printf("%s\n", name); //prints "John Q Public"
}
Declaring strings in C is easy: it's just an array of char.
Char array vs. char pointer array- specific question (noob learning C)
[C] printing out a character array returns gibberish
How to properly store and use char array in C / Arduino
'\n' as a character in a 2d array : r/C_Programming
Videos
Hi everyone, so I'm learning the basics of C using some online resources, and came across the following example of using a char pointer array:
const int MAX = 4;
int main (){
char *names[] = {
"Zara Ali",
"Hina Ali",
"Nuha Ali",
"Sara Ali"
};
int i = 0;
for ( i = 0; i < MAX; i++) {
printf("Value of names[%d] = %s\n", i, names[i] );
}
return 0;
}
And I just can't understand why making it an array (and not a pointer array) won't work, aka why just removing the * results in an error while running the code.
Could someone please dumb it down for me?