Ele975
Ele975

Reputation: 372

Reverse order in array of char in C

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.

Upvotes: 1

Views: 434

Answers (1)

Edwin Buck
Edwin Buck

Reputation: 70909

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);
}

Upvotes: 3

Related Questions