Question:
C programming: need help with arrays?
Steffie
2013-02-25 22:44:14 UTC
Is char* ar[] the same as char** ar?

If so, then by logic:

char** ar[] is the same as char*** ar?

ar[] itself is already a pointer, right?

If I pass char** ar into a function, then char** ar in this case is the pointer to a pointer to a char variable stored in ar, correct? If I pass char* ar[] into a function, then char* ar[] is the pointer to a string stored in array ar[], right?
Three answers:
Fred
2013-02-25 23:11:49 UTC
Is it the same thing? Technically, not. An array is NOT a pointer. An array is implemented internally as a pointer, but an array is NOT equivalent to a pointer. (If you don't believe me take the sizeof an array and of a pointer to the same data type as that array.



With that being said, you do indeed have the correct idea.



char *ary[] as you put it, is indeed the way to declare an array of strings. The key difference here is that you will need to give this array a size at compile time. char** can be made to act as an array using dynamic memory allocation (malloc, calloc etc.) BUT it is also has another purpose. It is indeed a pointer to a pointer which means that if it appears in a function prototype it has a dual meaning. The function may accept a two-d ary, OR a pointer by reference. The same way you pass an integer by reference.



If you pass a char** into a function then if that char pointer points to a valid character pointer which then in turn points to a character than yes that is true, however, you will need to note the difference conceptually between a 2-D array and a table of values. You should think of a 2-D array as a serpate row of pointers to arrays

const char ary[10][10];



ary[0] = "Hello" assigns the entire string to this array



ary[0] --------> ary[][0] which is H. Note that this is not the correct syntax, but for idea's sake consider the first number the value of the first pointer I.E. to which array it points. Then after being dereferenced, you are working with a char* so you can think of it as an array of char at that point.



You final last question. Funtion prototypes do not distinugish between char* ar[] and char**. They can be treated differently by the programmer within the funcition, but as far is it is concerned it is receiving a pointer to a character pointer. How you increment, dereference it is up to you.
James Bond
2013-02-25 22:51:34 UTC
Yes in some way

char x[20];

char *p=(char *) malloc(20);

here, x and p are 1-D character arrays. Here, x is constant pointer while p is dynamic pointer. On p we can do p--, p++ etc, where as not on x.



In that sense what you have asked is equivalent.

Thus,

main(int argc, char **argv)

and

main(int argc, char *argv[])

are equivalent
goggans
2016-08-09 06:58:16 UTC
Int a,e,i,o,u,inta,inte,; char c[30]; for(i=0;i<30;i++) take input; swich c[i] case 'a': a++; retailer i in seperate integer array inta case e,i,o,u similarly case '#': goto next next: then print a,e,i,o,u then print seperate integer arrays this may do i assume.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...