11

Possible Duplicates:
Difference between char *str=“STRING” and char str[] = “STRING” ?
C: differences between pointer and array

Hi,

Can anybody tell me the difference between the statements below?

char *p = "This is a test";

char a[] = "This is a test";
1

3 Answers 3

8

When you declare char p[] you are declaring an array of chars (which is accessible to be both read and written), and this array is initialized to some sequence of characters i.e. "This is test" is copied to the elements in this array.

When you declare char* p, you are declaring a pointer that points directly to some constant literal - not a copy. These can only be read.

Sign up to request clarification or add additional context in comments.

2 Comments

char *p = "literal" is ill-formed.
@SimonRichter not in C89
5

a is an array which means that you can use the sizeof() operator on a and sizeof(a)/sizeof(a[0]) is equal to the array length.

p is a pointer to a constant memory zone.

Comments

3

1 - pointer which points to the read only section of the program containing "This is test\0" string.

2 - memory (13 bytes) which is initialized with contents mentioned above.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.