-8

I am trying to understand this algorithm, which reverses a C-style character in-place. I don't understand what the * indicates in the context of being before a string and in the context of "char * end." Thanks for your help!

void reverse(char *str) {
    char * end = str;
    char tmp;
    if (str) {
        while (*end) {
        ++end;
        }
        --end;
        while (str < end) {
            tmp = *str;
            *str++ = *end;
            *end-- = tmp;
        }
    }
}
Kick Buttowski
  • 6,631
  • 13
  • 35
  • 57
Chloe
  • 11

3 Answers3

0

the asterisk refers to a pointer

char tmp

this is a character

char * str

this is a pointer to a char (or char array in this case).

tmp = *str;

means that the character tmp is filled with the first character from the string array pointed to by the pointer str.

Chris Maes
  • 30,644
  • 6
  • 98
  • 124
  • 2
    `this is a pointer to a char array.` No, it's a pointer to a character. That character may or may not be the first character in a character array. – Iskar Jarak Nov 07 '14 at 07:44
0

You are really interested in learning then you need to learn basic of c first then pointer:

This one is a very quick tutorial of pointers : http://www.programiz.com/c-programming/c-pointers then go through this and see string as pointer : https://www.cs.bu.edu/teaching/c/string/intro/

I will suggest to go through them once help you in understanding many things in other languages also. :)

Chirag Jain
  • 1,602
  • 13
  • 20
0

It's a character pointer when you use a "" before a string.. This is also treated as character array where the string entered after"" will be the name of that array.