Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to initialize a pointer in c?

Tags:

c

pointers

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?

like image 408
diesel Avatar asked Sep 26 '11 18:09

diesel


2 Answers

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

char_pointer = array_thing;

or

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.

like image 120
Carl Norum Avatar answered Nov 19 '22 00:11

Carl Norum


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

like image 1
Daniel Brockman Avatar answered Nov 18 '22 23:11

Daniel Brockman