Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between char **p,char *p[],char p[][]

Tags:

c

char *p = "some string"   

creates a pointer p pointing to a block containing the string.

char p[] = "some string" 

creates a character array and with literals in it.

And the first one is a constant declaration.Is it the same of two-dimensional arrays?

what is the difference between

char **p,char *p[],char p[][]. 

I read a bit about this that char **p creates an array of pointers so it has an overhead compared to char p[][] for storing the pointer values.

the first two declarations create constant arrays.i did not get any run time error when i tried to modify the contents of argv in main(int argc,char **argv). Is it because they are declared in function prototype?

like image 215
programer8 Avatar asked Oct 11 '13 18:10

programer8


People also ask

What is the difference between char * p and char * p?

char* p and char *p are exactly equivalent. In many ways, you ought to write char *p since, really, p is a pointer to char . But as the years have ticked by, most folk regard char* as the type for p , so char* p is possibly more common.

What is the difference between char a [] string and char * p string?

char a[]="string"; // a is an array of characters. char *p="string"; // p is a string literal having static allocation. Any attempt to modify contents of p leads to Undefined Behavior since string literals are stored in read-only section of memory.

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.

Is char * the same as char [] in C?

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.


1 Answers

Normal Declarations (Not Function Parameters)

char **p; declares a pointer to a pointer to char. It reserves space for the pointer. It does not reserve any space for the pointed-to pointers or any char.

char *p[N]; declares an array of N pointers to char. It reserves space for N pointers. It does not reserve any space for any char. N must be provided explicitly or, in a definition with initializers, implicitly by letting the compiler count the initializers.

char p[M][N]; declares an array of M arrays of N char. It reserves space for MN char. There are no pointers involved. N must be provided explicitly. M must be provided explicitly or, in a definition with initializers, implicitly by letting the compiler count the initializers.

Declarations in Function Parameters

char **p declares a pointer to a pointer to char. When the function is called, space is provided for that pointer (typically on a stack or in a processor register). No space is reserved for the pointed-to-pointers or any char.

char *p[N] is adjusted to be char **p, so it is the same as above. The value of N is ignored, and N may be absent. (Some compilers may evaluate N, so, if it is an expression with side effects, such as printf("Hello, world.\n"), these effects may occur when the function is called. The C standard is unclear on this.)

char p[M][N] is adjusted to be char (*p)[N], so it is a pointer to an array of N char. The value of M is ignored, and M may be absent. N must be provided. When the function is called, space is provided for the pointer (typically on a stack or in a processor register). No space is reserved for the array of N char.

argv

argv is created by the special software that calls main. It is filled with data that the software obtains from the “environment”. You are allowed to modify the char data in it.

In your definition char *p = "some string";, you are not permitted to modify the data that p points to because the C standard says that characters in a string literal may not be modified. (Technically, what it says is that it does not define the behavior if you try.) In this definition, p is not an array; it is a pointer to the first char in an array, and those char are inside a string literal, and you are not permitted to modify the contents of a string literal.

In your definition char p[] = "some string";, you may modify the contents of p. They are not a string literal. In this case, the string literal effectively does not exist at run-time; it is only something used to specify how the array p is initialized. Once p is initialized, you may modify it.

The data set up for argv is set up in a way that allows you to modify it (because the C standard specifies this).

like image 93
Eric Postpischil Avatar answered Nov 15 '22 19:11

Eric Postpischil