简体   繁体   中英

memcpy character pointer array

char **gbuffer = NULL;
void dump(char **buffer)
{
    int i;
    buffer = realloc(buffer, 50 *sizeof(char **));

    for(i = 0 ; i < 10; i++)
    {
      buffer[i] = malloc(50 *sizeof(char));
      buffer[i] = gbuffer[i];
    }

    printf("string is %s\n", buffer[0]);  //able to access here
}
int main()
{
    char **buffer = NULL;

    gbuffer = realloc(gbuffer, 50 *sizeof(char **));
    int i;
    for(i =0 ;i < 10; i++)
    {
      gbuffer[i] = malloc(50 * sizeof(char));
      strcpy(gbuffer[i], "ashish");
    }

    dump(buffer);
    printf(" global string is %s\n", gbuffer[0]);
    printf("string is %s\n", buffer[0]);            //not able to access this value
    return 0;
}

I am able to access buffer in dump() but unable to access variable in main. How can I access buffer[0] in main()? I have attached pseudo-code.

It looks like your code in main that allocates gbuffer works correctly. It looks like your function dump is trying to do the same thing. It looks like, within dump , it correctly allocates buffer . But then, finally, back in main , buffer is improper.

That's because dump allocates memory and stores it in dump 's copy of the buffer pointer, but it doesn't get propagated back to main .

(This is a very common problem. By rights I shouldn't answer it; I should point you to one of the many hundreds of duplicate questions.)

One way to fix it is to have dump return the new value of buffer . I have also changed dump 's name, since it does much more than just dump the buffer it's passed. I have also fixed several other problems, which I'll explain later.

char **gbuffer = NULL;

char ** reallocate_and_dump(char **buffer)
{
    int i;
    buffer = realloc(buffer, 50 * sizeof(char *));

    for(i = 0; i < 10; i++)
    {
      buffer[i] = malloc(50);
      strcpy(buffer[i], gbuffer[i]);
    }

    printf("string is %s\n", buffer[0]);

    return buffer;
}

int main()
{
    char **buffer = NULL;

    gbuffer = realloc(gbuffer, 50 * sizeof(char *));
    int i;
    for(i = 0; i < 10; i++)
    {
      gbuffer[i] = malloc(50);
      strcpy(gbuffer[i], "ashish");
    }

    buffer = reallocate_and_dump(buffer);
    printf(" global string is %s\n", gbuffer[0]);
    printf("string is %s\n", buffer[0]);
    return 0;
}

You were saying things like buffer = realloc(buffer, 50 * sizeof(char **)) , but that's not quite right. buffer holds 50 char * 's. So you want buffer = realloc(buffer, 50 * sizeof(char *)) .

You were saying things like buffer[i] = malloc(50 * sizeof(char)) , but multiplying by sizeof(char) is unnecessary, because sizeof(char) is, by definition, exactly 1. So I have changed those to buffer[i] = malloc(50) .

You had the pair of lines

buffer[i] = malloc(50 * sizeof(char));
buffer[i] = gbuffer[i];

Here you allocate 50 bytes of memory and then immediately waste it by overwriting the pointer with a different pointer out of the gbuffer array. This is confusing and probably not what you want. I have replaced this with

buffer[i] = malloc(50 * sizeof(char));
strcpy(buffer[i], gbuffer[i]);

which I think makes more sense.


The other possibility, the other way to have reallocate_and_dump update and return the pointer it's handed, is to have it accept a pointer to that pointer. But since the pointer it's handed is already a two-level pointer, we end up with a three-level pointer, and I don't know about you, but three-level pointers have a tendency to make my head explode. Nevertheless, here's what that code looks like:

void reallocate_and_dump_2(char ***buffer_pointer)
{
    int i;
    char **buffer = *buffer_pointer;

    buffer = realloc(buffer, 50 * sizeof(char *));

    for(i = 0; i < 10; i++)
    {
      buffer[i] = malloc(50);
      strcpy(buffer[i], gbuffer[i]);
    }

    printf("string is %s\n", buffer[0]);

    *buffer_pointer = buffer;
}

int main()
{
    char **buffer = NULL;

    gbuffer = realloc(gbuffer, 50 * sizeof(char *));
    int i;
    for(i = 0; i < 10; i++)
    {
      gbuffer[i] =  malloc(50 * sizeof(char));
      strcpy(gbuffer[i], "ashish");
    }

    reallocate_and_dump_2(&buffer);
    printf(" global string is %s\n", gbuffer[0]);
    printf("string is %s\n", buffer[0]);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM