Using pointers to iterate through argv[]
Asked Answered
Q

6

7

I want to use the following code, but without indexing the array with"[][]" and substitute it with pointers

for (int i = 0; i < argc; i++) {
    for (int j = 0; argv[i][j] != '\0'; j++) {
        //code
    }
}

I know that you can use pointers to traverse an array, but I'm unsure how to do that with an undefined length in the second array, in this case the string from input. Since each element of argv[] can have a different length, I want to make sure that I can properly read the characters and know when each element of argv[] ends, and the next begins.

I expect it to be something like: (If the following header to main is wrong, please tell me.)

int main(int argc, char **argv) {
    for (int i = 0; i < argc; i++) {
        while(argv != '\0') {
            //code
            *argv+1;
        }
        //to skip null character
        argv+1;
    }
}
Quadruplicate answered 24/6, 2015 at 1:2 Comment(2)
You seem to be assuming that argv is a contiguous block of memory. I don't think that's a safe assumption.Salyers
@CareyGregory - is right. You cannot simply assume that the next element is right after the null byte. But you can safely assume that each element are of same size, therefore it will be safe to assume that elements are from argv to argv + argc - 1.Upthrow
S
6

Given that the last element in argv is NULL, you don't need to index it or compare anything with argc if you really don't want to.

int main(int argc, char *argv[]) {
  for (char **arg = argv; *arg; ++arg) { // for each arg
    for (char *p = *arg; *p; ++p) {      // for each character
      process(*p);
    }
  }
}

*arg will be NULL at the end of the list of arguments, which is false. *p will be '\0' at the end of each string, which is false.

From N1256 5.1.2.2.1/2

If they are declared, the parameters to the main function shall obey the following constraints:

— The value of argc shall be nonnegative.

— argv[argc] shall be a null pointer.

Sendai answered 24/6, 2015 at 2:2 Comment(0)
T
5

Since for loop allows any kind of values, not necessarily integers for your "loop index", your loop could be rewritten like this:

for (char **a = argv ; a != argv+argc ; a++) {
    for(char *p = *a ; *p != '\0' ; p++) {
        // code uses *p instead of argv[i][j]
    }
}

The inner loop uses p as the loop variable, which is incremented with the regular p++, and checked with *p != '\0'. The loop condition could be shortened to *p, so the inner loop would look like this:

for(char *p = *a ; *p ; p++)
Teeter answered 24/6, 2015 at 1:6 Comment(2)
How would you eliminate indexing with argv[i]? Perhaps I should have removed the upper for loop, to make it more clear. What you wrote will go to each individual sub-array from argv[][]. Since it is all treated as a linear 1D array, it should be possible to iterate without argc I think. Part of it would be finding the end of the line, so outer loop would be to "\n"Quadruplicate
@BlakeLassiter Who said it's all a linear 1D array?Salyers
S
3
#include <stdio.h>
int main(int argc, char **argv)
{
    printf("%i\n",argc);
    while (*argv != NULL) {
        printf("%s\n",*argv);
        argv++;
    }
    return 1;
}
Schilling answered 13/9, 2019 at 4:50 Comment(1)
You should explain your code, instead of posting a code-only answerAbandoned
U
2

Yes you can iterate through argv using pointers.

The inner loop tells p to point at the beginning of argv+i and iterate through it until it reaches \0.

#include <stdio.h>
int main(int argc, char **argv) {
    int i;
    char *p;
    for(i=0; i < argc; i++) {
        for(p=*(argv+i); *p; p++)
            printf("%c", *p);
        printf("\n");
    }
}

If you are only interested in traversing the each arguments, but not interested in parsing each character, then you can simply.

#include <stdio.h>
int main(int argc, char **argv) {
    int i;
    char *p;
    for(i=0; i < argc; i++) {
        printf("Argument position %d is %s\n", i, *(argv+i));
    }
}
Upthrow answered 24/6, 2015 at 1:24 Comment(0)
I
1

You can use any any_t * to iterate any any_t[], in this case:

int main(int argc, char **argv) {

  char **i = argv; //iterator of argv
  char **e = argv + argc; //end of argv

  for(; i != e; ++i) {

    char *j = *i; //iterator of *i

    //condition: *j mean *j != 0
    for(; *j; ++j) {
      printf("%c", *j);
    }

    printf("\n");

  }

  return 0;

}
Iveson answered 24/6, 2015 at 1:37 Comment(2)
Good answer. But the OP is asking for c not c++.Upthrow
@CareyGregory That just a example for nested loop, note that the question is about nested loop, not about "how to print a sequence". What I do is use printf instead of //code lineIveson
C
-1

Keep in mind that the argv[] is essentially an pointer array, which means that a *(argv+i) will add i * pointer size to the base addr( in this case is argv ) and deterrence it into a pointer to a string. Then, you could manipulate each pointer to a string at will. The following code is an example of printing out all the strings in argv:

int main(int argc, char *argv[]){
     int i;
     for(i = 0; i < argc; i++)
         printf("%s\n", *(argv+i));
     return 0;
}
Cookery answered 24/6, 2015 at 1:19 Comment(2)
What's the point of for loop here when it will iterate thru all the argv on the first pass. What will the succeeding passes do? SEGFAULT?Upthrow
Thanks @Upthrow for pointing out. The while loop in my previous answer is error prone. I have updated my answer.Cookery

© 2022 - 2024 — McMap. All rights reserved.