15

Possible Duplicates:
What is the difference between char s[] and char *s in C?

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

Community
  • 1
  • 1
Blue Sky
  • 151
  • 1
  • 1
  • 3

5 Answers5

29

The first one is array the other is pointer.

The array declaration "char a[6];" requests that space for six characters be set aside, to be known by the name "a." That is, there is a location named "a" at which six characters can sit. The pointer declaration "char *p;" on the other hand, requests a place which holds a pointer. The pointer is to be known by the name "p," and can point to any char (or contiguous array of chars) anywhere.

The statements

char a[] = "hello";
char *p = "world";

would result in data structures which could be represented like this:

   +---+---+---+---+---+---+
a: | h | e | l | l | o |\0 |
   +---+---+---+---+---+---+
   +-----+     +---+---+---+---+---+---+
p: |  *======> | w | o | r | l | d |\0 |
   +-----+     +---+---+---+---+---+---+

It is important to realize that a reference like x[3] generates different code depending on whether x is an array or a pointer. Given the declarations above, when the compiler sees the expression a[3], it emits code to start at the location "a," move three past it, and fetch the character there. When it sees the expression p[3], it emits code to start at the location "p," fetch the pointer value there, add three to the pointer, and finally fetch the character pointed to. In the example above, both a[3] and p[3] happen to be the character 'l', but the compiler gets there differently.

You can use search there are tons of explanations on the subject in th internet.

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Incognito
  • 16,567
  • 9
  • 52
  • 74
  • 4
    where is the memory allocated in the first case and where in the second case? stack? heap? when you decide to point p to somethng else does that mean that "wrold" will be deallocated? – Laz May 30 '10 at 14:54
  • 3
    @Ram Bhat: in the first case, it depends on where that definition is placed; if it's in a function, probably on the stack, if it is in a struct, it depends from where the whole struct is allocated; if it's outside any function, in the global vars segment. The same holds for the pointer p; the "world" string to which p points, instead, *usually* is in a particular section of the executable which is mapped in memory at loading, which is used as string table. – Matteo Italia May 30 '10 at 18:59
10

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.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
3

No difference. Unless you want to actually write to the array, in which case the whole world will explode if you try to use the second form. See here.

jasonmp85
  • 6,749
  • 2
  • 25
  • 41
2

First declaration declares an array, while second - a pointer.

If you're interested in difference in some particular aspect, please clarify your question.

Fyodor Soikin
  • 78,590
  • 9
  • 125
  • 172
1

One difference is that sizeof(a)-1 will be replaced with the length of the string at compile time. With p you need to use strlen(p) to get the length at runtime. Also some compilers don't like char *p="string", they want const char *p="string" in which case the memory for "string" is read-only but the memory for a is not. Even if the compiler does not require the const declaration it's bad practice to modify the string pointed to by p (ie *p='a'). The pointer p can be changed to point to something else. With the array a, a new value has to be copied into the array (if it fits).

travis0xFF
  • 11
  • 1