119

Can anyone explain me what is a difference between these lines of code

char *p = "String";
char p2[] = "String";
char p3[7] = "String";

In what case should I use each of the above ?

5

4 Answers 4

51

Strings in C are represented as arrays of characters.

char *p = "String";

You are declaring a pointer that points to a string stored some where in your program (modifying this string is undefined behavior) according to the C programming language 2 ed.

char p2[] = "String";

You are declaring an array of char initialized with the string "String" leaving to the compiler the job to count the size of the array.

char p3[5] = "String";

You are declaring an array of size 5 and initializing it with "String". This is an error be cause "String" don't fit in 5 elements.

char p3[7] = "String"; is the correct declaration ('\0' is the terminating character in c strings).

Reference

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

1 Comment

Just a note that the link has been updated to c-faq.com/charstring/index.html
50

This link should satisfy your curiosity.

Basically (forgetting your third example which is bad), the different between 1 and 2 is that 1 allocates space for a pointer to the array.

But in the code, you can manipulate them as pointers all the same -- only thing, you cannot reallocate the second.

1 Comment

guys, before you bookmark the link that fge posted, be aware that FAQ website has been updated to this c-faq.com and the specific link posted to this c-faq.com/aryptr (2.1,2.2) but without changes. Just in case.
22

You shouldn't use the third one because its wrong. "String" takes 7 bytes, not 5.

The first one is a pointer (can be reassigned to a different address), the other two are declared as arrays, and cannot be reassigned to different memory locations (but their content may change, use const to avoid that).

12 Comments

char p3[5] = "String"; while dangerous is not wrong and it is valid in C (but not in C++)
@ouah - it is wrong. It may pass compilation, but it is nevertheless wrong.
this is a strictly conforming definition for an object. A strictly conforming program is not "wrong" in terms of C.
@ouah Blatant runtime errors are wrong.
@Pacerier please stop being intentionally pedantic. You know very well that my statement doesn't mean 'renders the program useless'. The surrounding code is irrelevant. If I write to memory that I shouldn't write to, that's bad behavior. That's a bug. Even if no one notices, that's still a bug. Even if my code never runs, there's still a bug present. The bug may not run, but that doesn't mean my code is bug-free. Stop with the games, you're not being clever.
|
7
char *p = "String";   means pointer to a string type variable.

char p3[5] = "String"; means you are pre-defining the size of the array to consist of no more than 5 elements. Note that,for strings the null "\0" is also considered as an element.So,this statement would give an error since the number of elements is 7 so it should be:

char p3[7]= "String";

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.