why we need *names[] rather than names[] here, when I define it as const char names[],it will not execute.
#include <stdio.h>
#include <stdlib.h>
const int MAX = 4;
int main()
{
const char *names[] = {
"dggg",
"ggq",
"gg2",
"g23",
};
for ( int i = 0; i < MAX; i )
{
printf("Value of names[%d] = %s\n", i, names[i] );
}
return 0;
}
CodePudding user response:
A string in C is a = one dimensional character array. For instance
const char string[3] = "abc";
What the example code has is an array of C strings i.e.
{dggg", "ggq",....}
Without the pointer case, it would be like
#include <stdio.h>
#include <stdlib.h>
//const int MAX = 4;
#define NUMBER_OF_STRING 4
#define MAX_STRING_SIZE 40
int main()
{
const char names[NUMBER_OF_STRING][MAX_STRING_SIZE] = {
"dggg",
"ggq",
"gg2",
"g23",
};
for ( int i = 0; i < NUMBER_OF_STRING; i )
{
printf("Value of names[%d] = %s\n", i, names[i] );
}
return 0;
}