If I use char *s[]
, I can do the following:
char *s[] = {"foo", "bar", "foobar", "whatever", "john", "doe"};
What is the difference with **s
? How do I use the char **s
instead of char *s[]
in that case?
Example: int main (int argc, char **argv)
instead of *argv[]
The fundamental difference is that in one char* you are assigning it to a pointer, which is a variable. In char[] you are assigning it to an array which is not a variable.
Difference between char s[] and char *s in CThe s[] is an array, but *s is a pointer. For an example, if two declarations are like char s[20], and char *s respectively, then by using sizeof() we will get 20, and 4. The first one will be 20 as it is showing that there are 20 bytes of data.
char* is a pointer to a character, which can be the beginning of a C-string. char* and char[] are used for C-string and a string object is used for C++ springs. char[] is an array of characters that can be used to store a C-string.
C uses char type to store characters and letters. However, the char type is integer type because underneath C stores integer numbers instead of characters.In C, char values are stored in 1 byte in memory,and value range from -128 to 127 or 0 to 255.
For function parameters, there is no difference.
Otherwise:
char *s[];
s is an array of pointer to char. And
char **s;
s is a pointer to pointer to char.
You use the former if you need an array of pointer to char, and you use the latter if you need a pointer to a pointer to a char.
As function parameters, there is no difference whatsoever. They are equivalent.
void f(int** p);
void f(int* p[]);
void f(int* p[42]);
These three declarations are equivalent.
As objects, they have different types;
char *s[] = {"foo", "bar", "foobar", "whatever", "john, "doe"};
s
is an array of 6
pointers to char
sizeof(s) == 6*sizeof(void*)
char **s;
s
is a pointer to pointer to char
. sizeof(s) == sizeof(void*)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With