-3

So I cannot figure out this pointer code in C.Basically it removes numbers from a string.I am confused as why some places *src is used and some places it is not.Also please explain how this code works.

char* RemoveDigits(char* input)
{
    char* dest = input;
    char* src = input;

    while(*src)
    {
        if (isdigit(*src)) 


       { src++; continue; } //here we are using src and not *src.
        *dest++ = *src++;
    }
    *dest = '\0';
    return input;
}
aschepler
  • 70,891
  • 9
  • 107
  • 161
  • what does while(*src) give and why do I have to end *dest=\0 – Leo B Jacob Nov 04 '17 at 22:31
  • "Also please explain how this code works." Is not a question. Imo, learning C by picking and choosing at code bits and trying to understand how they work without fundamental knowledge is ineffective and maybe even damaging, I suggest taking a look at [The Definitive C Book Guide and List](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) :-) – George Nov 04 '17 at 22:31
  • A pointer is simply a variable that holds the *address of* something else as its value. In other words, a pointer *points to* the address where something else can be found. For example, while `int a = 5;` stores the immediate value `5` as its value, `int *b;` creates a pointer to `int`, and `b = &a;` stores the address of `a` (where `5` is currectly stored) as its value. If you need the value stored at the address `b` points to, you *dereference* `b` using the unary `'*'` operator, e.g. `int c = *b;` will initialize `c = 5`). – David C. Rankin Nov 04 '17 at 22:46

3 Answers3

2

Short answer:

Go study pointers, it's really important and too tricky to explain in just few lines of answer. Once you do that go to long answer.

Long answer:

char* RemoveDigits(char* input) {
   char* dest = input;
   char* src = input;
   while(*src) { 
      if (isdigit(*src)) { 
         src++; continue; 
      } //here we are using src and not *src.
      *dest++ = *src++;
   }
   *dest = '\0'; return input;
}

To understand this code you need to get how char arrays work in C. If you declare 'char[5] example = "test";' the variable 'example' will be a pointer to the first char of the array, which means that it will contain the memory address in which in this case the 't' character is stored.

If you type '*example' instead you're basically saying "give me the value stored at the memory address specified in the variable 'example', which will practically mean "give me the first letter", as a matter of fact if you type printf('%c', *example) it will show 'T'.

Once you understand this it will be easy to guess that by saying "example++" you are changing from the address of the 'T' to subsequent next value in memory 'E' and so on...

Now let's move to another point, how does this while works? This is pretty simple to understand once you get how arrays of char are managed in C. Each stored arrays of char always and with the '\0' character. Every character if placed in while statement will be considered as 'true' except for '/0' which will be considered 'false'. By checking the value stored in src and incrementing it at the end of the while you're saying "check all the character till the end"

Last thing: if the current character '*src' is a digit character just pass to the next character and restart the while without execute the bellow code, else if it's a relevant character write it to the destination array '*dest = *src' and then increment 'dest++' and 'src++' to prepare space for the next character.

I know this is a bit complicated the first time, comment if you need more help. Anyway on the internet there are a lot of good tutorial.

Luca Reccia
  • 578
  • 3
  • 16
1

A pointer in C is declared using an asterisk. A pointer is a primitive data type that holds a pointee (or address) to (of) a variable. To access the data pointed by the pointer, we dereference the pointer using again the asterisk.

The pointer could point to (Hold the address of) the first character of a char array (String) which is the case in your code snippet.

src++ this particular instruction makes the pointer point to the next character in the array.

*src this istruction derefernces the pointer to access the character it points to.

Karim Manaouil
  • 1,177
  • 10
  • 24
0
  • The src pointer is used to advance input to find non digits
  • The dst pointer is used to advance input after non digit is copied to it
  • Because resulting string is equal or smaller than original, null termination '\0' is added at the end.
  • The input pointer is intact and contains the start of the string, which is what you need and what is returned

Here is walk through: () - src pointer, [] - dst pointer :

{[(1)], 2, 'a', 'b', '\0'}
dst == input, src == input

{[1], (2), 'a', 'b', '\0'}
dst == input, src == input + 1

{[1], 2, ('a'), 'b', '\0'}
dst == input, src == input + 2
*dst = *src // copy because *src is not a digit

{'a', [2], 'a', ('b'), '\0'}
dst == input + 1, src == input + 3
*dst = *src // copy because *src is not a digit

{'a', 'b', ['a'], 'b', ('\0')}
dst == input + 2, src == input + 4
*src == false // no more copying, loop ends
dst* = '\0'; // terninating null added

{'a', 'b', ['\0'], 'b', ('\0')}
  ^ - input pointer
Killzone Kid
  • 6,171
  • 3
  • 17
  • 37