-1
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.

5
  • 2
    does this even compile? Commented Mar 7, 2018 at 4:08
  • You didn't allocate any memory for your buffer. Commented Mar 7, 2018 at 4:09
  • I know this, i just want to know how can i access the value of buffer I am new to C Commented Mar 7, 2018 at 4:11
  • memcopy(&buffer, &gbuffer, sizeof(gbuffer)); is same as buffer = gbuffer; Commented Mar 7, 2018 at 4:18
  • I have attached pseudo code please see it.and help Commented Mar 7, 2018 at 4:26

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks , It was useful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.