2

I'm trying to do something like this

typedef struct _thingy_t
{
    int num;
} thingy_t;

void go(int idk)
{
    // normally I could just do
    thingy_t* ary[idk];
    // but I need the array to be global    
}

I need an array of pointers to structs of size idk

Is using a 'double pointer' declared outside of the function the best way to go about this? And what about malloc'ing space for the structs?

10
  • Where do you see "double pointer"? You'd better allocate memory: thingy_t *array = calloc(idk, sizeof(thingy_t));. Commented Oct 22, 2013 at 4:51
  • @Eddy_Em but the array would be specific to the function then, right? I need it to be global. Commented Oct 22, 2013 at 4:54
  • @Eddy_Em He nees an array of pointers.. Commented Oct 22, 2013 at 4:55
  • 1
    There's another way: you can return pointer to you array from function. So there's at least 2 ways: 1) thingy_t *array; ... void go(int idk){... array = calloc(idk, sizeof(thingy_t)); ...}; and 2) thingy_t *go(int idk){... thingy_t *array = calloc(idk, sizeof(thingy_t)); ... return array;}; and somewhere else: thingy_t *ar = go(size); Commented Oct 22, 2013 at 5:03
  • 2
    You can't return a pointer to a VLA from a function; the VLA goes out of scope. If you want a variable-length array at global scope, or if you want to return a pointer from the function, you'll need to use dynamic memory allocation. If a fixed size array is adequate, you can avoid dynamic memory allocation. Commented Oct 22, 2013 at 5:06

1 Answer 1

3

You can declare as global and then allocate memory inside function.

if you want to allocate memory for array.

void go(int);  

thingy_t *data=NULL; 
main()
{

   //read idk value.
   go(idk);

}
void go(int idk)
{
        data = malloc(idk * sizeof(thingy_t) );
       // when you allocate memory with the help of malloc , That will have scope even after finishing the function.

}   

if you want to allocate memory for array of pointers.

thingy_t **data=NULL;

int main()
{
    int i,idk=10;
    go(idk);

    for(i=0;i<10;i++)
       {
       data[i]->num=i;
       printf("%d ",data[i]->num );
       }

return 0;
}

void go(int idk)
{
   int i=0;
   data=malloc(idk *sizeof( thingy_t * ));
   for ( i = 0; i<idk ; i++) {
      data[i]=malloc(sizeof( thingy_t));
   }
}  

Don't forgot to free() the memory which is allocated with malloc.

Sign up to request clarification or add additional context in comments.

4 Comments

> thingy_t **data=NULL; ?? You sure?
thingy_t **data = NULL; was correct, he wants an array of pointers to thingy_t, not an array of thingy_t. It was sizeof(thingy_t) which was wrong, should have been sizeof(thingy_t*), or even better, sizeof(*data).
@PaulGriffiths Yes, You are correct, OP wants array of pointers. modified my answer.
Do yourself a big favor and modify the name to distinguish that it is global, e.g "g_data", otherwise the OP will be posting a question within 2 days because his pointer goes missing and/or changes to it don't take hold :)

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.