Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I declare and initialize an array of pointers to a structure in C?

I have a small assignment in C. I am trying to create an array of pointers to a structure. My question is how can I initialize each pointer to NULL? Also, after I allocate memory for a member of the array, I can not assign values to the structure to which the array element points.

#include <stdio.h>
#include <stdlib.h>

typedef struct list_node list_node_t;

struct list_node
{
   char *key;
   int value;
   list_node_t *next;
};


int main()
{

   list_node_t *ptr = (list_node_t*) malloc(sizeof(list_node_t));

   ptr->key = "Hello There";
   ptr->value = 1;
   ptr->next = NULL;

   // Above works fine

   // Below is erroneous 

   list_node_t **array[10] = {NULL};      

   *array[0] =  (list_node_t*) malloc(sizeof(list_node_t));
    array[0]->key = "Hello world!";  //request for member ‘key’ in something not a structure or union
    array[0]->value = 22;            //request for member ‘value’ in something not a structure or union 
    array[0]->next = NULL;           //request for member ‘next’ in something not a structure or union


    // Do something with the data at hand
    // Deallocate memory using function free 

   return 0;
}
like image 443
user246392 Avatar asked Apr 15 '10 01:04

user246392


People also ask

How do you declare an array of pointers to structures?

Syntax: int *var_name[array_size]; Declaration of an array of pointers: int *ptr[3];

What is array of pointers how it is initialized in C?

An array of pointers is useful for the same reason that all arrays are useful: it lets you numerically index a large set of variables. Below is an array of pointers in C that points each pointer in one array to an integer in another array. The value of each integer is printed by dereferencing the pointers.

Can we declare pointer array in C?

C. In this program, we have a pointer ptr that points to the 0th element of the array. Similarly, we can also declare a pointer that can point to whole array instead of only one element of the array. This pointer is useful when talking about multidimensional arrays.


2 Answers

Here:

list_node_t **array[10] = {NULL};

You're declaring an array of 10 pointers to pointers to your struct. What you want is an array of 10 pointers to your struct:

list_node_t *array[10] = {NULL};

It's confusing because yes, array really is a pointer to a pointer, but the square bracket notation sort of abstracts that away for you in C, and so you should think of array as just an array of pointers.

You also don't need to use the dereference operator on this line:

*array[0] =  (list_node_t*) malloc(sizeof(list_node_t));

Because C dereferences it for you with its bracket notation. So it should be:

array[0] =  (list_node_t*) malloc(sizeof(list_node_t));
like image 174
Paige Ruten Avatar answered Oct 05 '22 20:10

Paige Ruten


The line list_node_t **array[10] = {NULL}; is wrong - here you declare array of pointers to pointers to list nodes. Replace that with:

list_node_t *array[10] = { NULL };

and it should work.

like image 38
Nikolai Fetissov Avatar answered Oct 05 '22 20:10

Nikolai Fetissov