1

I realise this is an incredibly noob question, but I've googled for it and can't seem to find an answer (probably because I've worded the question wrong... feel free to fix if I have)

So I have this code:

int main(int argc, char* argv[])
{
    puts(argv[1]);
    return 0;
}

It works fine if I've passed a parameter to my program, but if I haven't, then obviously it's going to fail since it's trying to index a non-existent element of the array.

How would I find how many elements in in my string array?

4 Answers4

7

That's what argc is for. It holds the number of elements in argv. Try to compile and run this:

#include <stdio.h>
int main(int argc, char* argv[]) {
    int i;
    if (argc < 2) {
        printf ("No arguments.\n");
    } else {
        printf ("Arguments:\n");
        for (i = 1; i < argc; i++) {
            printf ("   %d: %s\n", i, argv[i]);
        }
    }
    return 0;
}

Test runs:

pax> ./prog
No arguments.
pax> ./prog a b c
Arguments:
   1: a
   2: b
   3: c

The argv array ranges from argv[0] (the name used to invoke the program, or "" if it's not available) to argv[argc-1]. The first parameter is actually in argv[1].

The C++ standard actually mandates that argv[argc] is 0 (a NULL pointer) so you could ignore argc altogether and just step through the argv array until you hit the NULL.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Oh. I can't believe I did not see that... I feel like a total idiot now :( –  Nov 23 '09 at 06:27
  • `int *pKsEnd = (int *) calloc(strlen(pString), sizeof(int));`. I want to know the number of elements stored in this memory block. How can I know that? As `sizeof(pKsEnd)` will always return `4` on my machine. **EDIT:** You should also know that I'm using it as an array, does this affects my program? – phougatv Jul 23 '15 at 10:37
  • 1
    @barnes, that's a question rather than a comment. Had it not been asked before, I'd suggest asking it as a question so as to get more exposure. However, since it _has_ been asked before, I suggest you search SO, where you would find an answer like http://stackoverflow.com/questions/1598773/is-there-a-standard-function-in-c-that-would-return-the-length-of-an-array/1598867#1598867. – paxdiablo Jul 23 '15 at 12:35
  • `UNION` trick is really nice. But I have an idea and I think it is pretty cumbersome. Since, I'm using `calloc` to allocate memory so every block will be initialized to `0`. Now I can use a loop to find the length with the condition: `while(pKsEnd[r] != 0){ printf("%i", pKsEnd[r++]); }`. I really am not sure to use it or not. May be your sublime advice can help me out here. – phougatv Jul 23 '15 at 13:28
  • 1
    @barnes, I wouldn't do that, there is _no_ guarantee the the memory after an allocated block will be non-zero and it's undefined behaviour to dereference the memory beyond what you asked for. Hence you'll strike UB _before_ you can figure out you're beyond the end, even if you _can_ figure out you're beyond the end :-) – paxdiablo Jul 23 '15 at 14:04
1

That's what argc is.

for (int j = 0;  j < argc;  ++j)
    puts (argv [j]);
return 0;

This will correctly print all arguments/

wallyk
  • 56,922
  • 16
  • 83
  • 148
0

argc is a number of parameters. note that your app's name is a parameter too ;-)

Zepplock
  • 28,655
  • 4
  • 35
  • 50
  • Technically argv[0] is note necessarily the app name. It is an OS specific string that represents the application (which may or may not be absolute or relative). – Martin York Nov 23 '09 at 06:34
  • Agreed. Considering the content of the question - I was trying to simplify ;-) – Zepplock Nov 23 '09 at 21:43
0

The answer is contained in argc. NumOfParamaeters = argc-1;

Traveling Tech Guy
  • 27,194
  • 23
  • 111
  • 159