-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,709
  • 13
  • 37
  • 58
Chloe
  • 11
  • 1

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.

Evaa
  • 283
  • 1
  • 2
  • 15
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • 3
    `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,612
  • 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.