4

Possible Duplicate:
C - Difference between “char var[]” and “char *var”?
Difference between char a[]=“string”; char *p=“string”;

would someone explain what exactly the difference between char[] and char* is? for example difference between

char name[] = "earth";

and

char *name = "earth";

thanks

Community
  • 1
  • 1
user1012169
  • 41
  • 1
  • 1
  • 2

5 Answers5

7
char namea[] = "earth";
char *pname = "earth";

One is an array (the name namea refers to a block of characters).

The other is a pointer to a single character (the name pname refers to a pointer, which just happens to point to the first character of a block of characters).

Although the former will often decay into the latter, that's not always the case. Try doing a sizeof on them both to see what I mean.

The size of the array is, well, the size of the array (six characters, including the terminal null).

The size of the pointer is dependent on your pointer width (4 or 8, or whatever). The size of what pname points to is not the array, but the first character. It will therefore be 1.

You can also move pointers with things like pname++ (unless they're declared constant, with something like char *const pname = ...; of course). You can't move an array name to point to it's second character (namea++;).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 3
    `char *name = "earth"` is deprecated in C++03.It **Must** be `const char *name = "earth"`. Also it is apt to add that modifying an String literal *in anyway* leads to an UB. – Alok Save Oct 25 '11 at 09:26
2
(1) char name[] = "earth";

name is an character array having the contents as, 'e','a','r','t','h',0. The storage location of this characters depends on where name[] is declared (typically either stack or data segment).

(2) char *name = "earth";

name is a pointer to a const string. The storage location of "earth" is in read-only memory area.

In C++, this is deprecated and it should be const char *name = "earth";

iammilind
  • 68,093
  • 33
  • 169
  • 336
1
  1. char name[]= "earth"; creates a mutable array on the stack with the size of 6 with the value earth\0.
  2. char* name = "earth"; defines a pointer to a string constant with the value earth\0.
MarianD
  • 13,096
  • 12
  • 42
  • 54
Skyler Saleh
  • 3,961
  • 1
  • 22
  • 35
0

char[] describes an array of char with a fixed number of elements.

char* describes a pointer to a char, typically followed in memory by a sequence of char's typically terminated by a null char \0

John Weldon
  • 39,849
  • 11
  • 94
  • 127
0

With

char *name = "earth"

you must not modify the contents of name.

Hence

name[2] = 'A';

char* is terminated by a '\0' character while name[] has fixed size.

will cause a segfault.

Initializing the variable takes a huge performance and space penalty for the array. Only use the array method if you intend on changing the string, it takes up space in the stack and adds some serious overhead every time you enter the variable's scope. Use the pointer method otherwise.

krammer
  • 2,598
  • 2
  • 25
  • 46