2

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?"

1
  • Related, if not the same (in the sense of how to declare a pointer to an array): stackoverflow.com/q/9779450/694576 Commented Nov 30, 2014 at 14:01

4 Answers 4

3

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;
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

why you chooe typdef?
Because declaring a "pointer to an array of 20 void pointers" directly would result in complicated syntax. When I use the typedef, it means I get the type of myarr and put it as myarr_t, afterwards I can just say "give me a pointer to type of myarr".
1
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

Comments

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

6 Comments

why not void* prtToArray = myarr_t; ?
To hold address of normal variable you need pointer, to hold address of pointer you need pointer to pointer.
So what i wrote to you (void* prtToArray = myarr_t;) it illegal?
name of array represents pointer to element zero. arr[10] then arr is arr = &arr[0]
@user3729959 It would have been legal if you write void prtToArray = myarr_t[0];
|

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.