What is the difference between the following initialization of a pointer?

char array_thing[10];

char *char_pointer;

what is the difference of the following initialization?

1.) char_pointer = array_thing;

2.) char_pointer = &array_thing

Is the second initialization even valid?

  • 3
    @k-ballo Not sure how you're coming to the conclusion that it's homework. – MGZero Sep 26 '11 at 18:58
  • it's not homework..i'm just trying to learn C.. – diesel Sep 26 '11 at 19:00
  • 1
    @K-ballo: Not everyone who is asking basic questions is trying to cheat through academic informatics, there are some people out there who actually want to learn. – orlp Sep 26 '11 at 19:06
  • 1
    @K-ballo: That's why I started my comment with "If", I wasn't accusing anyone, it just struck me as homework. – K-ballo Sep 26 '11 at 19:08
  • 3
    +1 for asking. Understanding stuff like this is important if you want to learn C. – R.. Sep 26 '11 at 23:08

The second initialization is not valid. You need to use:

char_pointer = array_thing;


char_pointer = &array_thing[0];

&array_thing is a pointer to an array (in this case, type char (*)[10], and you're looking for a pointer to the first element of the array.

  • Thanks! this is what i thought. – diesel Sep 26 '11 at 19:00
  • The second initialization may still work, though, but it should throw a warning about the pointer types being incompatible. – Dmitri Sep 26 '11 at 21:09
  • 1
    @Dmitri, pointer conversions (except to or from void *) without explicit typecasts are illegal in C. 6.5.4 Cast operators, paragraph 3: "Conversions that involve pointers...shall be specified by means of an explicit cast." – Carl Norum Sep 26 '11 at 22:03
  • @CarlNorum, That may be but some compilers will allow it. I tried it with gcc, and it gave a warning, but compiled and ran fine. So it's not proper, but "may still work". – Dmitri Sep 26 '11 at 22:15
  • gcc allows lots of things that are invalid C, and that's a large part of the reason so much C code (especially FOSS) is total crap... – R.. Sep 26 '11 at 23:07

See comp.lang.c FAQ, Question 6.12: http://c-faq.com/aryptr/aryvsadr.html


Note that there are no initializations at all in the code you posted. That said, you should keep in mind that arrays decay to pointers (a pointer to the first element within the array). Taking the address of an array is certainly valid, but now you have a (*char)[10] instead of a char*.

  • 1
    Arrays (i.e., expressions of array type) decay to pointers in most contexts (as you implicitly acknowledge). The exceptions are: the operand of a unary & operator, the operand of a unary sizeof operator, and a string literal in an initializer used to initialize an array object (e.g., char s[] = "hello";). – Keith Thompson Sep 26 '11 at 19:13

In the first case, you're setting char_pointer to the first element of array_thing (the address of it, rather). Using pointer arithmetic will bring you to other elements, as will indexing. For example

char_pointer[3] = 'c';

is the same as

char_pointer += 3; char_pointer = 'c';

The second example...I don't believe that's valid the way you're doing it.

Your Answer

