3

Assume I have char **argv. How do I determine its size?

I have a string - an example would be: sleep 30 & that is held in argv. I would like to be able to access the last array in *argv. In this case, the last array contains &. How can I access it? strlen(argv) doesn't seem to work properly. sizeof() obviously wouldn't work properly because **argv is a pointer.

Note: I am not talking about **argv as an argument in main(), therefore, I do not have argc or any other indicator of how long the string is.

darksky
  • 20,411
  • 61
  • 165
  • 254
  • @vireshas that doesn't allow you to access the last array of *argv. Just shows you its length. – darksky Mar 14 '12 at 02:35
  • I already tried it - doesn't work. – darksky Mar 14 '12 at 02:38
  • @vireshas That sounds ... off. There is a difference between `NUL` and `NULL` (and `char*` and `char**`). –  Mar 14 '12 at 02:39
  • This is silly. OP is basically asking "How do I know where the end of a list is?" The answer is either that you remember how long the list was, or you have a sentinel value that comes after the last member. – M.M Apr 10 '14 at 01:45

4 Answers4

5

EDIT: Edited to work with a custom array of strings. A NULL pointer indicates the end of the array. Although this declares an array of 4 strings, this method could be used with a dynamically sized array.

#include <stdio.h>

int main()
{
    char* custom[4] = { "sleep", "30", "&", NULL };
    int last;
    for (last = 0; custom[last + 1]; last++);
    printf("%i - %s\n", last, custom[last]);
    return 0;
}

// ./a.out
// > 2 - &

For this to work for you, you would have to edit your program to explicitly include an extra NULL string in your char** when you build it. Without that indicator, the address after the last string wouldn't necessarily be NULL, so you could include garbage in the count or cause a segmentation fault.

0eggxactly
  • 4,642
  • 1
  • 16
  • 16
1

Passing in a count like argc is the most common usage - but you say you don't have that.

Then the usual way is to have the last element of argv to point to NULL to indicate it is the last array element.

int argc = 0;
while (*argv++) {
 argc++;
}
peterept
  • 4,407
  • 23
  • 32
0

You may need to use strtok to tokenize the arguments and work through them until you have the last one.

Referemce for strtok.

torrential coding
  • 1,755
  • 2
  • 24
  • 34
  • Wouldn't that be quite stupid though when you don't really have to tokenize anything? – darksky Mar 14 '12 at 02:41
  • @Nayefc If you have to discard all the other tokens, yes. But, it's generally the case that all argument tokens are there to be consumed in some way or the other. – torrential coding Mar 14 '12 at 02:45
0
   char *argv[] = {"abc","123","xya"};

    //prints the last string
   printf("%s",a[strlen(*a)-1]);

// if you are sure that the array of strings ends with NULL

   char *temp = 0 ;
   while(*argv){
        temp = *argv;
        (*argv)++;
   }
  //temp has the last string
vireshas
  • 806
  • 6
  • 19
  • This makes the assumption that there is a "trailing" `NULL`. That should be documented, at least. (The post says it "not an argument ... in `main`".) –  Mar 14 '12 at 02:45
  • I am compiling with -Werror. This gives *argv++ an error that it is not being used afterwards. – darksky Mar 14 '12 at 03:01
  • @pst i edited the answer .. i have added a comment as u had mentioned :) – vireshas Mar 14 '12 at 05:25
  • `printf("%s",a[strlen(*a)-1]);` - this only works because your first string has the same number of characters in it as the list has strings. Change `"abc"` to `"supercalifragilistic"` and see what happens – M.M Apr 10 '14 at 01:44