2

Hello I am learning C and am following a tutorial, I have read up about pointers basics.

main(int argc, char *argv[])

I don't really understand how to interpret char *argv[].

Do I read it as CharPointer points to argv array, or pointer to chararray made of char?

[update]

I maybe wrong however I think I am confused as I am familiar with:

char* argv[] // pointer to char array 
char argv[]* // set value in pointee 

however I have never seen char *argv[].

einpoklum
  • 118,144
  • 57
  • 340
  • 684
laycat
  • 5,381
  • 7
  • 31
  • 46
  • 1
    try this [answer](http://stackoverflow.com/questions/4176326/arguments-to-main-in-c). I believe this has been asked before. – Cacho Santa Apr 22 '13 at 01:23
  • 1
    thanks for the share! Im interested in finding out how C takes in it's arguments if you could get a little more technical. – laycat Apr 22 '13 at 01:37

3 Answers3

2

You read C declarations from the inside out, keeping in mind that the primary operators ., ->, and [] have higher priority than unary operators like *. So you go left or right, inside out, starting from the mostly deeply nested, and choosing left or right on priority.

char *argv[]; // argv is an array of pointer to char
int (*f)();   // f is a pointer to a function returning int

Here is a complete example I just made that you can run:

int main(void);

// g is an array of pointer-to-function with no parameters returning int
int (*g[])(void) = {
  main,
  main
};

// f is an array of pointer-to-array of pointer-to-function returning int
int (*(*f[2])[2])(void) = {
  &g,
  &g,
};


int main(void) {
  return *f[0][0] == main;
}

$ ./a.out
$ echo $?
# => 1
DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
  • for the last one, I maybe wrong however I am reading it as, return f which is a 2d array of pointers. pointers please *pun intended* – laycat Apr 22 '13 at 14:56
0

You should read this as a char pointer to char array because of the way you defined it but don't forget there's no difference between any kind of pointers (char,int,void,float etc.) they're all adress variable (some kind of) .

yalinaksoy
  • 11
  • 3
0
char *argv[]

argv is the variable/parameter name, it is not a type, so CharPointer points to argv array is not the right way to understand it. It is OK to say some pointer to int array or double array since int or double are types.

char* argv[] is actually an array of char* (reads array of pointers to char), basically, an array of C-strings.

taocp
  • 23,276
  • 10
  • 49
  • 62
  • I maybe wrong however I think I am confused as I am familiar with: Char* argv[] // pointer to char array Char argv[]* // set value in pointee however I have never seen char *argv[] – laycat Apr 22 '13 at 01:51
  • @laycat I have never seen char argv[]*, but char * argv[] is very common in C++. where did you see char argv[]*? – taocp Apr 22 '13 at 01:54
  • I made a mistake I have seen a statement like pointerX 45* to change the value in the variable is is similar to char *argv[] ? – laycat Apr 22 '13 at 03:02