3

I have the following function:

 int func(int carg, const char **varg) { 
  //code ...
 }

My understanding is the following:

varg is a pointer to a second pointer. This second pointer points to a char. This char is ...

either: a single char (1st case)
or: a char that constitutes the first char of a char array. (2nd case)

Is this understanding correct?

The thing I don't understand is why you would want to pass a pointer to a pointer as an argument.

If you wanted to pass (by reference) a single char to the function, you could simply write:

int func(int carg, const char *v) { 
      //code ...
}

In the 2nd case, where you want to pass (by reference) a char array to the function, one could use the same function (where this time, v points to the first element of the array passed to the function):

int func(int carg, const char *v) { 
  //code ...
 }

In summary, I don't understand why you would want to use a pointer to a pointer as argument to a function.

The concrete case I have is the following declaration of a main function:

 int main(int carg, const char **varg);

**varg handles the arguments on the command line. One can access the command line arguments using varg[1], varg[2], etc. So, obviously, what **varg does is simply to save the command line arguments in an array. But this could be achieved in a more simple way with the following code:

 int main(int carg, const char *varg); 

What am I missing?

steady_progress
  • 3,311
  • 10
  • 31
  • 62
  • 2
    sorry, you need to be a 3 star programmer to understand these things. You need to level up. – bolov Nov 26 '16 at 15:43
  • Why is your question title a snippet of C code? – byxor Nov 26 '16 at 15:43
  • @bolov which books should I read to understand these things? (and to become a 3 star programmer)? – steady_progress Nov 26 '16 at 15:44
  • 1
    C is strictly pass-by-value! YOu cannot pass-by-reference in C. Pointers a first-class types. QWhat you ask will be answered in any good C book and has been answered here multiple times already. We are no tutoring site. – too honest for this site Nov 26 '16 at 15:45
  • You're right. This could be done with a single pointer. It was just the choice of whoever made C to do this. This question is similar to asking _"Why would anyone use a multi-dimensional array when they could just use a single array?"_. (I'm not saying that arrays and pointers are the same thing, this is just an analogy) – byxor Nov 26 '16 at 15:45
  • @Olaf ... you can use pointers to a variable when you want (de-facto) to pass the variable by reference, correct? – steady_progress Nov 26 '16 at 15:46
  • @BrandonIbbotson: `char **` is **not** a multidimensional array! It cannot point to one, nor represent one! And that's not "because it's designed that way, but the logical consequence from writing type-correct code. – too honest for this site Nov 26 '16 at 15:47
  • @Olaf I know it isn't and I never said it was. It was just an analogy. I've edited my comment to make this more clear. If you could remove yours, that would be great. – byxor Nov 26 '16 at 15:48
  • @steady_progress: No! You pas the **address** of the variable. There is a difference between references and pointers. As I wrote already: please read a good book. – too honest for this site Nov 26 '16 at 15:49
  • @BrandonIbbotson ... I can think of several use-cases where a 2-dimensional array would be useful ... and it's not equivalent . But as far as I can see using a pointer to a pointer is equivalent to using just a pointer ... so I don't understand the point. – steady_progress Nov 26 '16 at 15:50
  • @steady_pogress: A pointer to a pointer is obviously a pointer. The difference is in what it points to. – Fang Nov 26 '16 at 15:51
  • @Olaf I'm reading "The C programming language" ... but I couldn't find an explanation there ... can you recommend a book explaining this stuff? – steady_progress Nov 26 '16 at 15:54
  • There are search-engines and even a book list here. Please search yourself. Note: while the K&R is outdated and should be accompanied by a book about modern C (C99, better C11), it is not bad. You might want to meditate about what you read. And don't skip chapters. – too honest for this site Nov 26 '16 at 16:11
  • It's easier to understand if it is like this: `int main(int carg, char *varg[])`. `varg` is an array of pointers to chars. –  Nov 26 '16 at 18:33
  • I've closed this as a duplicate of [What should `main()` return in C and C++](http://stackoverflow.com/questions/204476/) because that also covers the standard on the meaning of the command line arguments. The second argument, conventionally called `argv`, is a pointer to the first element of an array of `argc+1` (the conventional name for what's called `carg` here) `char *` values. Of these, `argv[0]` points to the program name and `argv[argc]` is a null pointer. There are rules for 'program name not available' etc. The key information is that `argv[i]` is a pointer to the start of a string. – Jonathan Leffler Nov 26 '16 at 21:10

1 Answers1

1

To store a string you need an array of chars in memory. Pointer to any array in C is [type of array elements] * which is essentially pointer to first element in this array. In case of strings it is char *.

Arguments of command line is array of strings. That means, you have an array, where each element is cell in memory, containing pointer to another array - array of char. In this case type of elements in this array of pointers to array is char *, what give us proper type of pointer to this array (just substitute char * to formula above): char **.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • Actually a pointer to array (of `char`) is `char (*)[]`. `char *` is a pointer to `char`. Which is the confusion OP has. – too honest for this site Nov 26 '16 at 16:21
  • 1
    This is the same things. char[] is same as char *. It is syntactic sugar – Andremoniy Nov 26 '16 at 16:23
  • @Olaf remember, that any `blabla[x]` is just `*(blabla + x)` and nothing more – Andremoniy Nov 26 '16 at 16:24
  • "char[] is same as char *" - Definitively not! Try `sizeof(char [37]) == sizeof(char *)`! Don't spread such nonsense! This is not about a specific implementation, but the language. – too honest for this site Nov 26 '16 at 16:29
  • In sense of pointers is same. `char[37]` not the same as `char *` as it is comparing of different structures – Andremoniy Nov 26 '16 at 16:30
  • There are no structures. – too honest for this site Nov 26 '16 at 16:31
  • By *structure* I mean more general term, not what `struct` defines as complex type. Do not carp to the words. – Andremoniy Nov 26 '16 at 16:33
  • 1
    It's not me trying to press his own vocabulary. There is a correct term, called _type_. And that is the crux here. You rely and argument with implementation specifics for a language question. Worse: without any need! Even for function parameters, your statements are not true. Remember this is read by beginners who tend to take those statements literally, so being correct is **vital**. There is already enough missconception in OP and many other beginners about arrays and pointers. You do no good spreading it! – too honest for this site Nov 26 '16 at 16:53
  • @Olaf Sorry for getting involved, but why don't you provide your own answer to the question? I consider myself a beginner (maybe advanced beginner) in C and would be delighted to hear it. – Fang Nov 26 '16 at 17:11