2D Arrays and Pointers - C -


just trying head round arrays , pointers in c , differences between them , having trouble 2d arrays.

for normal 1d array have learned:

char arr[] = "string constant"; 

creates array of chars , variable arr represent memory created when initialized.

char *arr = "string constant"; 

creates pointer char pointing @ first index of char array "string constant". pointer point somewhere else later.

char *point_arr[] = {     "one", "two","three", "four" }; 

creates array of pointers point char arrays "one, "two" etc.

my question

if can use both:

char *arr = "constant"; 

and

char arr[] = "constant"; 

then why can't use:

char **pointer_arr = {     "one", "two", "three", "four" }; 

instead of

char *pointer_arr[] = {     "one", "two", "three", "four" }; 

if try char ** thing error "excess elements in scalar initializer". can make char** example work allocating memory using calloc, didn't have char *arr = "blah";. don't see why necessary , don't understand difference between:

char **arr_pointer; 

and

char *arr_pointer[]; 

many in advance advice.

in short, cannot use { ... } initialiser scalar.

char **arr_pointer declares scalar, not array. in contrast, reason can char *arr = "constant"; because you're still declaring scalar, happens point @ string literal.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -