user2637293
user2637293

Reputation: 351

How to declare a pointer to : array of void pointers? in c

I'm little bit confused about how to declare a pointer to array or void*.

let say :

void* myarr[20];

how to declare a pointer to "myarr?"

Upvotes: 2

Views: 7681

Answers (4)

D3Hunter
D3Hunter

Reputation: 1349

void* (*myarr_ptr)[20] = myarr_ptr;

here is my test code:

#include <stdio.h>

int main()

{
    int* myarr[20];
    int * (*myarr_ptr)[20] = myarr;
    printf("%p %p\n", myarr, *myarr_ptr);

    return 0;
}

$ ./a.out 
0x7fff8bd39dd0 0x7fff8bd39dd0

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

I think you mean a pointer to the first element of an array of pointers to void.

It is simply to do if you will use the following general approach. Let's assume that you have an array of some type T

T myarr[20];

then the definition of the pointer to the first element of the array will look like

T *ptr = myarr;

Now all what you need is substitute T for you particulat type and you will get

void * myarr[20];

void * *ptr = myarr;

If you mean indeed a pointer to the array then approach is the following

T myarr[20];
T ( *ptr )[20] = &myarr;

Or if to substitute T for void * you will get

void * myarr[20];
void * ( *ptr )[20] = &myarr;

Upvotes: 3

Pranit Kothari
Pranit Kothari

Reputation: 9841

void** ptrToArrary = myarr_t; //pointing to base address of array.

Upvotes: -1

tohava
tohava

Reputation: 5412

typedef void *myarr_t[20];
myarr_t *ptr_to_myarr;

Upvotes: 3

Related Questions