-2

I saw some C code, and I'm not sure what it does.

*dest++ = *src++;

I do not have this code in a context unfortunately (I was reading an article), but I assume src and dest were defined earlier. So I know an asterisk on a variable definition means a pointer to the memory location of that variable. But what does the asterisk mean in the context of a computation?

Jazzwave06
  • 1,883
  • 1
  • 11
  • 19
  • It's C. `*` = pointer or multiplication, depending on usage. in this case, it's a pointer. – Marc B Feb 18 '15 at 16:54
  • 1
    It means to dereference the pointer (i.e. to access what the pointer is pointing at). This example will be from a simple version of `memcpy`. – slugonamission Feb 18 '15 at 16:54
  • ok so `*src` is the same thing as `src[0]`? – Jazzwave06 Feb 18 '15 at 16:55
  • @sturcotte06 Yes, they are the same thing. – axiac Feb 18 '15 at 16:57
  • Ok thank you. I don't know why my question got down rated, it's actually hard to find an answer to this question when all you have is a line of code and no word to describe it. – Jazzwave06 Feb 18 '15 at 17:02
  • possible duplicate of [Pointers in C: when to use the ampersand and the asterisk?](http://stackoverflow.com/questions/2094666/pointers-in-c-when-to-use-the-ampersand-and-the-asterisk) – dandan78 Feb 18 '15 at 17:13
  • @sturcotte06: You're question is getting downvoted because it can be answered by looking at any basic C reference (either online or on paper). – John Bode Feb 18 '15 at 17:25
  • If reading a reference was always the answer, then stackoverflow wouldn't exist. It's to dodge the hassle of reading 40 pages of a document to find what we want that we ask questions. – Jazzwave06 Feb 18 '15 at 17:29
  • @sturcotte06: ...aaaaand that's why your question is getting downvoted. – John Bode Feb 18 '15 at 20:43

4 Answers4

3

Here is a very common context* for an operation like this:

void strcpy(char *dest, const char *src) {
    while (*dest++ = *src++)
        ;
}

This is an implementation of C string copying algorithm. An asterisk in each expression means pointer dereference, i.e. obtaining the value stored at the location pointed to by the pointer.

* Second edition of K&R, page 88.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • It's unrelated, but this code would be faster (on 64bit architecture) if dest and src were casted to long int* before the loop, right? – Jazzwave06 Feb 18 '15 at 17:10
  • @sturcotte06 This specific piece of code expects the pointers to point to `char`, so that it could detect null terminator, which is a single char `'\0'`. An implementation of `memcpy` would benefit from a cast to `int` or `long long`, but then one would have to be careful not to "overshoot" the end of the buffer, and also to avoid dereferencing at an odd address (some architectures would trap that, and abort the program). – Sergey Kalinichenko Feb 18 '15 at 17:16
0

Easy to understand version

void strcpy(char* dest, char* src)
{
    while(*src != '\0')  // While the src-character is not at the end of the string
    {
        *dest = *src; // Copy one character from the src buffer to the destination bufffer
        src++;        // Move the src to the next character
        dest++;       // Move the dest to the next character.
    }
}

Short Version

void strcpy(char* dest, char* src)
{
    while(*src) *dest++ = *src++;
}
abelenky
  • 63,815
  • 23
  • 109
  • 159
0

The asterisk, like many other symbols in C, have multiple meanings depending on context. In a variable declaration it means the variable is a pointer, used in an expression like this it dereferences a pointer, that is it gets the value of what the pointer points to.

Then you have the postfix increment operator ++ which returns the value of the expression and then increases it, in this case it returns the pointer and then increases the pointer.

So what e.g. *src++ does is dereerence the pointer src to get its value, then increase the pointer (so it points to next location in memory).

As for the whole expression *dest++ = *src++ it simply copies the value pointed at by src to the value pointed at by dest then increase respective pointer. In short, it copies from src to dest. You will most likely see this in a loop.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

In long-winded form that avoids terms like "dereferencing", you can read this line as

  • src is a pointer containing the address of a memory location, which is presumably pointing to an element within an array of values of a specific type.
  • *src is the value stored in that specific location.
  • dest is also a pointer containing the address of a memory location, which is also presumably an element within an array of values of a specific type (which should be compatible with the type pointed to by src).
  • *dest is the value stored in that specific location.
  • Assign the value from the memory location pointed to by src to the memory location pointed to by dest; then increment both the src and dest pointer values by the size of their respective types, advancing both pointers to the next elements in their respective arrays.
Richard Schwartz
  • 14,463
  • 2
  • 23
  • 41