Double Pointer char Operations
Asked Answered
L

5

5

Assume I have char **argv.

First, how can I print out all the strings in argv? I tried the following:

char *temp;
temp = *argv; // Now points to the first string?
while (temp != NULL) {
    printf("%s ", temp);
    temp++;
}

In here, when temp is incremented, it only skips one character. Why is that happening? I know that argv is an array that holds points. Each pointer, points to an array of char*. If so, why isn't this working? I know that since temp is of type char, incrementing that pointer would increment it by 1 char (or byte). If so, how can I increment the pointer into the next array and print that string out?

Lippi answered 25/3, 2012 at 13:53 Comment(0)
E
7

It skips only one character because temp is a pointer to a char. By adding one, you're telling the compiler to move the pointer on to point at the next char in memory.

argv is an array of pointers. What you need to do is move on to the next pointer on each iteration. Something like:

char **temp = argv;  // temp is a pointer to a *pointer*, not a pointer to a *char*
while (*temp != NULL) {
    printf("%s ", *temp);
    temp++;
}
Epicanthus answered 25/3, 2012 at 13:56 Comment(0)
S
6

You need to increment argv, not *argv. With a local copy, this looks like so:

for (char ** p = argv; *p; ++p)      // or "*p != NULL"
{
    char * temp = *p;                // now points to the first string!
    printf("%s ", temp);             // or just "printf("%s", *p);"
}
Sacking answered 25/3, 2012 at 13:55 Comment(5)
Is the last element of argv null? I would expect this to run off the end of the array.Housebound
@CollinHockey: Yes, the standard requires it. See 5.1.2.2.1 of the C99 standard.Epicanthus
@CollinHockey: Going by the OP's abortive attempt I would assume so, and also because there's no other obvious termination condition in sight. Oli: the OP didn't say that we're dealing with the standard main argument, but let's all pretend we do.Sacking
@OliCharlesworth Good to know. I assumed he was using the standard argv/argc pair somewhere in main.Housebound
Nope not dealing with standard main argument, but it is NULL terminated.Lippi
H
1

First, you need to understand what char** argv is. It is an array of pointers to char. The pointers in this array don't necessarily reside anywhere near eachother in the address space. What you want is this:

char** temp;
temp = argv;
while(temp != argv + argc) {
    printf("%s ", temp);
    temp++;
}

You need to have a pointer to the first element of the array to pointers to char. Increment that instead.

Housebound answered 25/3, 2012 at 13:57 Comment(1)
There is no argc. It is not standard main argument.Lippi
W
0

You have to increment argv not *argv. Note that if your argv is the parameter of the main function it is modifiable and you can use it like this:

    while (*argv++) {
        printf("%s\n", *argv);
    }
Whereas answered 25/3, 2012 at 14:0 Comment(1)
If he only needs to print the strings value, this is the most compact form.Whereas
O
0

what you probably want to do is this:

char* a = argv[0];  // first arg
char* b = argv[1];  // second arg
char* c = argv[2];  // third arg

which is equivalent to this:

char* a = *(argv + 0);
char* b = *(argv + 1);
char* c = *(argv + 2);

which you would then want to generalise into a loop.

Okeechobee answered 25/3, 2012 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.