1

The pointers confuse me a lot. I have a function that takes as arguments argc (the number of argument that are strings + 1 that is the name of the code) and char* argv[], that, if I understood well, is an array of pointers. Now as result I need to print on each line the argument (string) and the reversed string. This means that if I pass as arguments hello world, I need to have:

hello olleh
world dlrow

I tried to implement a part of the code:

int main(int argc, char *argv[])
{
    int i = 1;
    int j;

    while (i < argc)
    {
        while (argv[i][j] != 0) {
            printf("%c", argv[i][j]);
            j++;
        }
        
        i++;
    }
    return 0;
}
}

And now I'm literally lost. The inner loop doesn't work. I know that argv[i] pass through all my arguments strings, but I need obviously to enter in the strings (array of chars), to swap the pointers. Also I don't understand why the difference between argv[0] and *argv, because in theory argv[0] print the first element of the array that is a pointer, so an address, but instead of this it prints the first argument.

1
  • 5
    Ask yourself, what value should j have at the beginning of the inner while loop. Commented Oct 3, 2020 at 15:07

1 Answer 1

3

char* argv[] is a "array of pointers to a character" It's important to learn how to read types in C; because, those types will enable / thwart your ability to do stuff.

Types are read right to left. [] is a type of array, with an unspecified number of elemnts * is a pointer type, char is a base type. Combine these, and you now have "array of pointers to a character"

So to get something out of argv, you would first specify which element it is in the array. argv[0] would specify the first element. Let's look at what is returned. Since the array is not part of the result, the answer is "a pointer to a character"

 char* line = argv[0];

would capture the pointer to a character, which is stored in argv[0].

In C a char* or a "pointer to a character" is the convention used for a string. C doesn't have a new type for "string"; rather it uses pointers to characters, where advancing the pointer eventually runs into the \0 character that signals the string's end.

int main(int argc, char* argv[]) {
   int index = 0;
   while (index <= argc) {
      char* line = argv[index];
      printf("%d, %s\n", index, line);
      index++;
   }
 }

should dump the arguements passed to your program. From there, I imagine you can handle the rest.

Notice that I never converted the pointer to an array. While arrays can be used as pointers if you never specify the index of the array, in general pointers can't be used as arrays unless you rely on information that isn't in the type system (like you clearly grabbed the pointer from an array elsewhere).

Good luck!

---- Updated to address the question "How do I reverse them?" ----

Now that you have a simple char* (pointer to a character) how does one reverse a string?

Remember a string is a pointer to a character, where the next characters eventually end with a \0 character. First we will need to find the end of the string.

char* some_string = ...
char* position = some_string;
while (*position != 0) {
  position++;
}
// end_of_string set, now to walk backwards
while (position != some_string) {
  position--; 
  printf("%c", *end_of_string);
}
Sign up to request clarification or add additional context in comments.

4 Comments

So how can I enter inside the argument to iterate over the chars?
@ElenaFranchini walk the chars once. Without doing that, you'll never know where the characters end. Then start at the end and walk the characters back to the beginning, either storing them in reverse order; or if storage isn't necessary, printing them to the screen. I'll update the post to give a few poitners.
Thank you, sorry for the late answer, is a little bit clear, but there is something that it's confusing. Let's say that I have a string p1="hello", and on the other hand I have an array of strings argv containing "hello" "world". Now if I print p1, I have the string "hello", so I though that printing argv, I should have the addresses of hello and of world, but instead I have only the address of hello, why? I mean, printing the pointer must return what it contains, so why P1 contains all the array and argv contains only the address of the first element even though they point both to arrays?
@ElenaFranchini The next address in the array of strings will contain "world". Separate out the array part in your mind from the string part. The array is still just an array; but, it is an array of pointers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.