Assign value to array pointer

Viewed 264

In C, if I have a pointer to an array of pointers, how do I assign a pointer to an object to be element of the array of pointers?

I tried something like this: (Note: arrayPointer is a pointer to an array of pointers)

(*arrayPointer)[i] = pointer;

But it didn't work. It gives me seg fault core dumps. Then I tried this:

(*arrayPointer + i) = pointer;

It does not compile.

Here is an example similar to my question:

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

typedef struct node{
    int index;
} node;

node* newNode(int i){
    node* new = malloc(sizeof(node));
    return new;
}

// Create a pointer to an array of node pointers
node *(*create(int N))[]{
    int i;
    node *(*node)[N];

    for(i = 0; i < N; i++){
        (*node)[i] = newNode(i);
    }
    return node;
}

int main(){
    node *(*nodes)[8] = create(8);
}

I used gdb to check:

Program received signal SIGSEGV, Segmentation fault.                         
0x0000000000400599 in create (N=8) at main.c:20                              
20              (*node)[i] = newNode(i); 
4 Answers
#include <stdio.h>
#include <stdlib.h>

int main()
{
        void **array;
        int *p1, *p2;
        int temp1 = 1, temp2 = 2;

        p1 = (int *)malloc(sizeof(int));
        p2 = (int *)malloc(sizeof(int));

        p1 = &temp1;
        p2 = &temp2;

        array[0] = p1;
        array[1] = p2;

        printf("\np1 = %p, p2 = %p, *p1 = %d, *p2 = %d", p1, p2, *p1, *p2);

        printf("\n array[0] = %p, array[1] = %p", array[0], array[1]);
        return 0;
}

The above code is an example of array of pointers.

Output :

p1 = 0x7ffffb7a6968, p2 = 0x7ffffb7a696c, *p1 = 1, *p2 = 2

array[0] = 0x7ffffb7a6968, array[1] = 0x7ffffb7a696c

You don't need to call the value that arrayPointer points to, rather just call its first element.

Consider the following example:

int foo = 1;
int* pointer = &foo;
printf("pointer = %p\n", pointer);

int* arrayPointer[3]; // Arbitrary length
arrayPointer[0] = pointer; // Like in your code
printf("arrayPointer[0] = %p", arrayPointer[0]);

Which outputs:

pointer = 0x7ffc17b109b4
arrayPointer[0] = 0x7ffc17b109b4 // The same pointer

While the other answers suggest alternative implementations, they don't state clearly what the error in the example is: node *(*node)[N]; defines a pointer node, but does not initialize it, so node doesn't point to allocated memory and the subsequent *node in the line (*node)[i] = newNode(i); behaves undefinedly. To rectify this without changing the rest of the code, you can write

    node *(*node)[N] = malloc(sizeof *node);

instead.

The following worked for me: It is similar to what @armali suggested but not dynamic allocation.

// Create a pointer to an array of node pointers
node *(*create(int N))[]{
    int i;
    node *(*node)[N] = malloc(sizeof *node); // pointer to an array of node pointers

    for(i = 0; i < N; i++){
            (node_array)[i] = newNode(i);
    }
    return node;
}

The explanation goes like this:

node *(*node)[N]; // pointer to an array of node pointers

This is a pointer to an array of node pointers. So this needs to point an array of node pointers. Hence first declare

We allocate node pointers and save it in node*

node *(*node)[N] = malloc(sizeof *node);

You are allocating a new node in the call to newNode(), whose return value we are saving in the array of node pointers.

Related