3

been asked a question on this, basically coming up with argc...without actually having argc

if your given argv, which as I understand essentially a array of pointers to the relevant char arrays of each inputted argument,

how would I actually go about counting the number of pointers in argv?

SGE
  • 2,317
  • 3
  • 19
  • 16

5 Answers5

8

The C standard specifies:

argv[argc] shall be a null pointer.

So you can always detect the end by testing for 0.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
2

In general, you can't, unless the array is specifically terminated by some "stop" value like 0 or NULL. This is the reason argc exists. Pointers themselves don't have a length/count/number of elements associated with them.

When passing around pointers to arrays, you must necessarily also pass in the number of elements or explicitly allocate one-too-many elements and add a "null" element to the end of the array.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • @JensGustedt Read again please. I *specifically* said "In general", and answered in the context of *all arrays*, not specifically `argv`. – user229044 Nov 11 '11 at 18:44
  • @meegar, well, well, then your second phrase has not much context to refer to. `argc` exists for convenience (or tradition) and would strictly speaking not be necessary. – Jens Gustedt Nov 11 '11 at 20:14
  • +1 for pointing out the pattern of a zero(or whatsoever)-terminated array, which makes it *unecessary* to carry around a size descriptor with it. – alk Nov 12 '11 at 10:46
  • This answer doesn't fit the question. OP is asking about arg{v,c}, not "all arrays" – Robert Martin Nov 12 '11 at 15:55
  • @RobertMartin Actually, OP asked about "C arrays" specifically argv. My answer covers both. Read the title of the question: *"how to tell when you've reached the end of a C array? (specifically argv)"* – user229044 Nov 12 '11 at 17:22
  • Perhaps the answer should be updated to reflect that, though this answer is generally right, it is wrong in the current context. – Robert Martin Nov 12 '11 at 18:38
2

I disagree with some people here.

argv itself is null terminated. For example, if you type

$./foobar hello strawberry

you'll get

argv = [%p]  [%p]  [%p]  [NULL]
        |     |     |    
        |     |      \_"strawberry\0"
        |     | 
        |      \_"hello\0"
        |    
         \_"./foobar\0"

In other words, this sort of code will work:

while (*argv) {
    printf("%s\n", *argv);
    argv++;
}

Try it and see!

Robert Martin
  • 16,759
  • 15
  • 61
  • 87
  • You may be right about it ending in a NULL per Jens comment, but assuming that it will be a NULL without knowing the standard is a bad idea. – David Winant Nov 11 '11 at 17:07
  • [app.execve.01.02](http://linuxtesting.org/results/lsb_reqs?showreqs=execve) "argv: the application shall ensure that the last member of this array is a null pointer." – Robert Martin Nov 11 '11 at 20:12
1

You cannot. After the array ends there is randomness, and you cannot accurately distinguish randomness from real data.

drdwilcox
  • 3,833
  • 17
  • 19
0

You must use argc, which is always present when argv is furnished. The final char pointer in argv is at argv[argc-1]

Pete Wilson
  • 8,610
  • 6
  • 39
  • 51