Mail Archives: djgpp/1997/06/19/22:36:39
eyal DOT ben-david AT aks DOT com wrote:
>>> I've seen char **argv and char *argv[] and am curious which is correct
>Or
>>> are either correct?
>> They are both the same thing. The array syntax is interchangable with
>> pointer
>> arithmetic. Or at least should be if the compiler in question follows
>the
>> language at all.
>Not exact. The are both the same thing only when used as parameters
>to functions (like in main).
>char* argv1[] (not as a parameter) is an array of pointer
>variables.
>char** argv2 (always) is a pointer (variable holding the address of) a
>pointer-to-char.
>for example :
>extern char* argv1[]; /* declares array of pointers. */
>extern char **argv2;
>argv1++; // ERROR this is an array !!
>argv2++; // OK.
>so there is difference.
On the other hand you can use pointers as arrays (if they have
dynamically allocated space, so you could do:
x=*argv1[1]; // and
x=*argv2[1];
In fact I believe arrays are treated exactly like pointers, and as you
can see from the example, pointers can be indexed as arrays...
So in fact there's not much difference between arrays and pointers,
and none at all when used as arguments for main().
- Raw text -