0

CODE UPDATED BASED ON ANSWERS BELOW AND WARNINGS/ISSUES REFLECTED

I am trying to code an array of pointers in C. A loop sets the values and another loop retrieves them - fairly straight forward. Here's how I am trying to set those values:

static int  *toInvert[8];
for (i=0; i<8; i++)
{
              int *intrplated = //Function call that returns int*
              toInvert[i] = intrplated;
              //printf("OL Value = %d\n\n\n\n\n",oLoop);
}

And to retrieve the values, here's the code without the loop, i.e., retrieves a fixed value:

int *tmpPtr = toInvert[3]; 
printf( "*(TPointer + %d) : %d\n\n", 3, *(toInvert[3] + 1)); //Still gives the recently added value

What happens is when I try to print the values, only the last added values in the setter loop gets printed. Even if I change tmpPtr to toInvert[1], it will still get the last set values.

But if I run the same code within the written for loop, it works as expected.

I need to know how to retrieve all the values that have been set. Thanks

EDIT What I want is an array of 8 elements that contain 8 pointers. Each pointer in turn points to an array of 3 plain integers. The array that I want should be like [p1][p2]...[p8] where [p1] points to an array of ints.

7
  • 1
    Ah, an array of pointers. Such a simple concept, yet so complex in C. Commented Apr 26, 2013 at 18:35
  • 1
    I get 15,966 results when searching SE for "[c] array of pointers". (Im trying to suggest maybe your question has already been answered somewhere). Commented Apr 26, 2013 at 18:37
  • Why are you taking the address of the interplated variable? Commented Apr 26, 2013 at 18:39
  • 1
    You're storing the address of a local variable that ceases to exist when the loop body ends. Commented Apr 26, 2013 at 18:40
  • 1
    It's not even an array of pointers, it's an array of pointers-to-pointers! Commented Apr 26, 2013 at 18:40

7 Answers 7

2

What you are trying to implement is an Array of Pointers, right?

#include <stdio.h>
#include <stdlib.h>
#define LIMIT 3

int main()
{
    int i,j;
    static int  *toInvert[LIMIT];
    int *intrplated;
    for (i=0; i<LIMIT; i++)
    {
        intrplated = malloc(sizeof(int)*5);
        intrplated[0] = rand();
        intrplated[1] = rand();
        intrplated[2] = rand();
        intrplated[3] = rand();
        intrplated[4] = rand();
        toInvert[i] = intrplated;
    }
    for (i=0;i<LIMIT;i++)
    {
        printf("Tpointer to toInvert[%d] contains::\t [",i);
        for(j=0;j<5;j++)
        {
            printf("%d ",toInvert[i][j]);
        }
        printf("]\n");
    }

    putchar('\n');
    for(j=0;j<5;j++)
    {
        printf("%d ",toInvert[1][j]);
    }
    putchar('\n');
    for(i=0;i<LIMIT;i++)
    {
        free(toInvert[i]);
    }
    return 0;
}

Compiled on GCC 4.8.0

EDIT: See the change, the array of pointers retains the info

Output:

Tpointer to toInvert[0] contains::       [41 18467 6334 26500 19169 ]
Tpointer to toInvert[1] contains::       [15724 11478 29358 26962 24464 ] 
Tpointer to toInvert[2] contains::       [5705 28145 23281 16827 9961 ]

15724 11478 29358 26962 24464
Sign up to request clarification or add additional context in comments.

1 Comment

While the code you've written works, it still results in the same issue that I am facing,i.e., only the last set values are retrieved. For example, if I print toInvert[1][2] , it will end up printing toInvert[LIMIT][2]....coz they were the ones that were set in the end
1

You've declared an array of pointers to pointers to ints here:

static int  **toInvert[8];

Remove a * to get an array of pointersto ints instead. Then assign intrplated instead of &intrplated on each iteration. intrplated is already a pointer, so &intrplated is a pointer to a pointer. All put together:

static int *toInvert[8];
for (i=0; i<8; i++)
{
    int *intrplated = //Function call that returns int*
    toInvert[i] = intrplated;
}

7 Comments

I've updated the code based on your answer and it still gives the same issue - not retrieving any values except the last ones
@GauravWadhwani Can you be sure that the function you are calling is allocating new memory for each call? It could be reusing the same memory each time you call it in which case you would need to allocate your own memory on every iteration and memcpy the values into the memory you allocated.
You mean the function call that returns int*. There's no memory specific code there and that MAY be the issue. So how do I call that function using memcpy?
@GauravWadhwani If that function has no calls to malloc that is almost certainly the problem. I'll update my answer. One question first though, does that function return a pointer to a local variable (array declared in the function like int local[3];)? Using that pointer would be undefined behaviour in C, because the array doesn't exist after the function returns, in which case the function itself needs to be fixed, not the calling code.
The function has a declaration like static int r[4]; and then after its operations, it returns r..
|
1

This will work.

static int  *toInvert[8];
for (i=0; i<8; i++)
{
          int *intrplated = //Function call that returns int*
          toInvert[i] = intrplated;
          //printf("OL Value = %d\n\n\n\n\n",oLoop);
}

In your code, this

    static int  **toInvert[8];

means that you have a array of pointers to pointers.

This:

          int *intrplated = //Function call that returns int*
          toInvert[i] = intrplated;

is wrong. You are passing the address of the local variable intrplated to your array of pointer.

And this

int *tmpPtr = *toInvert[3];
printf( "*(TPointer + %d) : %d\n\n", 3, *(tmpPtr + 1));

is also wrong, it may work in your case because of the wrong declaration of toInvert , but is wrong. Look for more information about mixed pointers and array declarations.

Comments

1

You have too many *s throughout.

#include <stdlib.h>
#include <stdio.h>
int main() {

    static int  *toInvert[8];
    int i;
    for (i=0; i<8; i++)
    {
              int *intrplated = (int*) calloc(10, sizeof(int));
              toInvert[i] = intrplated;
              //printf("OL Value = %d\n\n\n\n\n",oLoop);
    }

    int *tmpPtr = toInvert[3];
    printf( "*(TPointer + %d) : %d\n\n", 3, *(tmpPtr + 1));

    return 0;
}

This is an array of ints:

int x[8];

This is an array of pointers to int:

int *x[8];

Comments

-1

Your function calls probably overwrites the pointer it returns. It should allocate this pointer using malloc and you should free it after you've used it.

You should also omit the & if the function returns a pointer.

Comments

-1

You don't need the & in:

toInvert[i] = &intrplated;

the right way is:

toInvert[i] = &intrplated;

Since intrplated is already a pointer to an integer you are taking the address of the pointer not the pointer itself (essentially a pointer to a pointer).

Remember pointers are more like integers that the compiler treats as a position of memory so them themselves are also stored somewhere and have their own address too.

Also this:

static int  **toInvert[8];

Is not a pointer of pointers, it's a pointer to an array of pointers. I believe what you actually want is this:

static int  *toInvert[8];

Comments

-1

This code:

for (i=0; i<8; i++)
{
    toInvert[i] = &intrplated;
}

is setting every element of toInvert to the address of intrplated. No matter which element of toInvert you look at you will dereference the same address and see whatever value is stored in intrplated.

Also, once the for loop exits, intrplated no longer exists so you will get undefined behavior when derferencing the values in the array.

This is what you would need if you want to store the pointers returned by the function.

for (i=0; i<8; i++)
{
    toInvert[i] = intrplated;
}

If you are storing int * you need an array of int * such as:

int * toInvert[8];

2 Comments

I'm sure I need an array of Pointers.
"Array of Pointers" or "Array of Array of Pointers" ?

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.