views:

117

answers:

5

hey guys right so i have been at this problem for the last 6 hours and have been hitting google like mad to no avail. Right I need a pointer to an array. This array contains pointers to Linked lists. Im going to have to malloc it since I dont know the array size until runtime.

LList **array

this was my first thought but this just gives me a pointer to an array of LList. Or atleast that is my understanding. Can someone give me a hand? Alex

EDIT: ok some info on how it would be used. Im implementing a very basic hash table. there is a structure that contains a pointer to an array of pointers to linked lists. it needs to be a pointer to the array so that when i resize the table, i can just change the pointer to point to the larger table.

A: 

A pointer to an object, is basically the same as a pointer to an array.

int * blah; // an int pointer. It could point to an array of ints, or a single int.
int ** blah; // a pointer to an int pointer. It could point to something that points to an int, or it could be pointing to an array of pointers to single ints, or it could be a pointer that points to an array of ints.

It all depends on how you use it.

McKay
+4  A: 

It sounds like you're on the right track.

LList **array;
array = malloc(num_ptrs * sizeof(LList*));

array is now an array of pointers to LList, and elements such as array[3] will be a pointer to a LList.

Arrays and pointers are very similar in C (but not identical!), as shown by the classic example: *(array + 2) is mostly equivalent to array[2].

Edit: When you need to resize the table, you'll just need to realloc the additional space:

LList **new_array;
new_array = realloc(old_array, new_size * sizeof(LList*));

new_array and old_array may or may not be the same pointer afterwards, but either way new_array is guaranteed to be a pointer to enough space to hold the new array (or NULL if the memory couldn't be allocated)

2nd Edit: As user411313 alluded, if you want the actual pointer to the array, you'll need to take the address of the array:

LList ***p_array;
p_array = &array;
wrong. the question was a pointer to an array of pointers to LList. your solution is only an array of pointers to LList.
fixed..........
A: 

if you have to write your own linked list, you can do this.

typedef struct LLNode {
    LLNode* next;
    int     data;
} LLNode;

LLNode* linkedList = null; // a linked list

LLNode**  linkedListArray = (LLNode**) malloc( arraySize* sizeof(LLNode*) );

LLNode*** pointerToLListArray = &linkedListArray;

with a linked list library:

LList*  linkedListArray = (LList*) malloc( arraySize* sizeof(LList) );

LList** pointerToLListArray = &linkedListArray;
Alex Reece
A: 

A pointer to a pointer can also be an array of pointers.


int nLists; /* number of lists*/
LList **array;
array = (LList **)malloc(nLists * sizeof(LList *));

will make array be an array of pointers to LList. Then array[i] will give you the pointer to the i-th linked list in the array.

Dima
A: 
typedef struct LList LList;
struct LList {
int value;
LList *next; };

LList *(*p)[3]; /* pointer to an array of 3 pointers to LList */
LList ll1 = {11};
LList ll2 = {22};
LList ll3 = {33};
size_t sizeofarray = sizeof*p/sizeof**p; /* calc arraysize at runtime here */
p = malloc( sizeofarray * sizeof**p ); /* allocate space for each LList-pointer in array */
(*p)[0] = &ll1;
(*p)[1] = &ll2;
(*p)[2] = &ll3;
/* test output here: */
printf("\n%d\n%d\n%d", ((*p)[0])->value,((*p)[1])->value,((*p)[2])->value);
free(p);