delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/19/22:36:39

From: istvan AT outer-net DOT com (Steve Marton)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: char **argv vs. char *argv[]
Date: Sun, 15 Jun 1997 13:58:51 GMT
Organization: Shaw FiberLink Limited
Lines: 43
Message-ID: <5o12i2$i64@news.fiberlink.net>
References: <422564B1 DOT 004E9870 DOT 00 AT aks DOT com>
NNTP-Posting-Host: outer-net.com
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019