Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

char **s versus char *s[], when and how to use each one?

Tags:

c

char

pointers

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[]

like image 535
Marcelo Araujo Avatar asked Jul 21 '11 20:07

Marcelo Araujo


People also ask

Should I use char * or char []?

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.

What's difference between char's [] and char * s?

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.

What does char * [] mean in C?

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.

What does char [] do in C?

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.


2 Answers

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.

like image 172
sidyll Avatar answered Sep 19 '22 01:09

sidyll


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*)

like image 38
Armen Tsirunyan Avatar answered Sep 18 '22 01:09

Armen Tsirunyan