r/cprogramming • u/Specific-Snow-4521 • Jan 01 '26
static char *list[] = { "John", "Jim", "Jane", "Clyde", NULL };
what the point of the star in the name list can't u just use it without the star and what the null doing
0
Upvotes
13
u/jean_dudey Jan 01 '26
char* is a pointer to a string, e.g. "John", the [] is to represent an array, you could use it for a single string though, like static char name[] = "John";, also the NULL at the end is just to mark the end of the array but that depends on how you use it, if you always expect that there's a NULL at the end you can iterate over the list without having an explicit length parameter somewhere.
7
u/SmokeMuch7356 Jan 01 '26 edited Jan 02 '26
listis an array of pointers tochar, hence the*.The string literals
"John","Jim", etc., are stored somewhere in memory (addresses are made up for illustration):There's no guarantee that they are stored in any particular order or that they are stored contiguously like this. They may be stored in read-only memory. The behavior on attempting to modify a string literal is undefined: it may work, it may crash outright, it may fail silently, it may start mining bitcoin.
liststores the addresses of these literals; given the layout above, the contents oflistwill beThe
NULLacts as a sentinel; it marks the end of the data in the array (in this case it's also the last physical element in the array, but it doesn't have to be). This allows you to write code likewithout having to know how many elements are in the array.
Strings are sequences of character values including a zero-valued terminator - the string "hello" is represented as the sequence
{'h', 'e', 'l', 'l', 'o', 0}.That terminator is also a sentinel; it's how various string handling routines like
strlenandstrcpyand evenprintfknow where the end of the string is.0works because it doesn't correspond to a printable character in ASCII/UTF-8 or EBCDIC.Strings are stored in arrays of character type:
or
gives you
All strings are stored in character arrays, but not all character arrays store a string; if there's no terminator, or there are multiple zero-valued bytes, then the array doesn't contain a string.
To store a list of strings (not pointers to strings), you need to create a 2D array:
This creates 5 character arrays of 6 elements each:
The empty string acts as a sentinel again. This time, we'd use it as
5
u/Ill-Language2326 Jan 01 '26
You COULD use "static char list[]", but it has a different meaning. "char*" is a pointer to a char. "char list[]" is an array of chars. "char* list[]" is a list of pointers to chars.
The first two are often used to store strings. C does not have a native string type. Strings are just an array of characters. I won't go into detail about their differences to not give you too much right now. The third is a list of strings. It means each entry in the list is a string. The "NULL" at the end is used to determine when the list ends. If you don't know the size, you wouldn't be able to know where the list ends. It has the same meaning as the ending null byte in strings.
2
u/CalebGT Jan 01 '26 edited Jan 02 '26
list is an array of pointers to characters. The string literals are of type char*. It would be more clear to put the * next to char instead of next to list. When you write "John", the compiler puts characters 'J' 'o' 'h' 'n' '\0' in a space near the beginning of the program used for constants. It then puts a pointer to the 'J' in your array named list at the first position. The * denotes pointer.
1
u/fatemonkey2020 Jan 01 '26 edited Jan 01 '26
1
u/pheffner Jan 01 '26
The declaration declares 'list' as a vector of character pointers, which usually point to null-terminated strings.
The initializer (within the curly brackets) is a list of such string which causes the compiler to set aside memory for those strings (and their null-byte terminators) and initialize the values of the list array with their addresses including the final one which will be set to zero and point to nothing.
The NULL is what's known as a sentinel value to signify no more values are available and you need not (and should not) access beyond this point.
In an accessor loop like this simple one which prints off the values in list, you see the NULL being used to stop the loop
int i = 0;
while (list[i] != NULL)
{ puts(list[i])'
++i;
}
I hope that's of some value!
1
u/SolidOutcome Jan 01 '26
[] and * are doing the same thing here. Pointers, but one is in array format[].
Pointer, just like array, can be a list of objects.
In this case it's a (list of a (list of (chars))).
A (list of chars) is a (string). A list of that is a (list of strings). In this case, an array of strings(same thing).
Null is ending the list by denoting a null character to hit when iterating the list
when I say list, I mean the human type, not the data structure type
1
u/pskocik Jan 01 '26
String literals (such as
"John") are static char '\0'-terminated arrays (reallystatic char constarrays but they're typed without theconst(=typed wrong) for legacy reasons) that decay tochar*(pointing at the first character)).static char *list[] = { "John", "Jim", "Jane", "Clyde", NULL };puts those
char*into a static list of pointers and it terminates the list with a(char*)NULL. The sentinel means you can pass just&list[0](normally obtained just by mentioninglist, via the array decay mechanism) and you don't need to pass the length because the length can be derived by traversing the array up until theNULL.For typesafety reasons, string-literal-derived pointers should normally be stored in
char const*pointers rather thanchar*pointers (reflecting the fact that the memory is readonly and the literal should really be typedchar const[], notchar[]), but a list like yours will likely be used as an argument to a legacy function likeexecv, which expects achar* const*(implicitly convertible fromchar**) even though it never modifies the eventual characters.If that's the case, the list could be at least
static char *const[], and thisconston the list (not the chars) would be beneficial (for safety and readonly memory sharing).1
u/zhivago Jan 01 '26
It allows for variable length strings.
1
u/sudheerpaaniyur Jan 07 '26
how to study like this stuff and allong with pointer array, where i can get maetrial.
char *list[], char *ptr=, char *test(){ return}
-1
u/Radstrom Jan 01 '26
Did you just write code and then ask us what's the point of it?
1
u/Hyddhor Jan 01 '26 edited Jan 01 '26
the star specifies that it's a list of strings, not a list of characters (ie. a string)
The
NULLat the end is just a delimiter telling you that the list has ended. This is needed because in C there is no way to get the length of a list during runtime (other than in specific cases where you are dealing with a const expression). You have to know where the list ends if you want to iterate it.ps: generally, better approach is storing length of the list, instead of delimiting it with
NULL.