0

I have a question on adding strings and data types. What does it mean to use them in such arithmetic? Does body + size mean that it is creating a new block of memory that includes both of them?

The following is just an excerpt of the code:

char* body;
ssize_t size = load();
char buffer[512];

    body = realloc(body, size + octets);
        if (body == NULL)
        {
        return -1;
        }
        memcpy(body + size, buffer, octets);
        size += octets;
1

The first argument to memcpy() is the destination, i.e., a pointer to the destination array where the content is to be copied.

So, keeping mind that a char* points to the first block of memory in an array, body + size is going to point to the "size-th" block of memory. Let's say body points to a block of 25 chars so body is storing the address of body[0] If size is 6, then body+size will point to the same as body[6].

You can see this in action quite simply by running this sample program:

#include <stdio.h>

int main (void)
{
    char* digits = "1234567890";
    printf("%s\n", digits+6);
}

// output is "7890"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .