By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,661 Members | 779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,661 IT Pros & Developers. It's quick & easy.

pointer to array of pointers

P: 4
Can anyone explain how to access the 1st element of 1st array in array of pointers?

suppose i have an array of 3 pointers, i define another pointer to this array of pointer. how do i access 1st element of 1st pointer in that array?
Dec 1 '15 #1
Share this Question
Share on Google+
7 Replies

Expert Mod 5K+
P: 9,197
An array of pointers is:

Expand|Select|Wrap|Line Numbers
  1. int* ptr[3];
so the 3 int pointers are ptr[0], ptr[1], and ptr[2].

OK. Now a pointer to this array is a pointer to an array of pointers. For all arrays, the name of the array is the ADDRESS of element 0. So you need:

Expand|Select|Wrap|Line Numbers
  1. int** val = ptr;
So val is the address of ptr[0]. Therefore, *val is ptr[0]. Therefore, **val is the int pointed at by ptr[0].
Dec 1 '15 #2

Expert 100+
P: 2,398
Pointer Arithmetic
When adding a pointer val and an in integer i, it is assumed that the pointer points somewhere into an array, call that the current position. The effect of adding integer i is to reference the ith object in the array relative to the current position.
Expand|Select|Wrap|Line Numbers
  1. *(val+0) refers to the same location as ptr[0]
  2. *(val+1) refers to the same location as ptr[1]
  3. *(val+2) refers to the same location as ptr[2]
Array Subscripting
Array subscripting is defined in terms of pointer arithmetic: a[i] is the same as *(a+i), thus:
Expand|Select|Wrap|Line Numbers
  1. val[0] is same as *(val+0) is same as ptr[0]
  2. val[1] is same as *(val+1) is same as ptr[1]
  3. val[2] is same as *(val+2) is same as ptr[2]
Dec 1 '15 #3

P: 4
these are the pointers i will be using to read my inputs stored in diff files.

int32 MIXER_R_InPntr = 0 ;
int32 MIXER_C_InPntr = 0 ;
int32 MIXER_Sw_InPntr = 0 ;
int32 MIXER_MIXERFL_InPntr = 0 ;
int32 MIXER_MIXERFR_InPntr = 0 ;
int32 MIXER_MIXERRL_InPntr = 0 ;
int32 MIXER_MIXERRR_InPntr = 0 ;


int32 *MIXER_IN_PNTR[6];

when i am building i am getting error message
error C2040: 'MIXER_IN_PNTR' : 'int32 *[6]' differs in levels of indirection from 'int32 [6]'.

Please Help.
Dec 2 '15 #4

P: 4
my_array[6] = {FL, FR, RL, RR, RL, C, Sw};
these array elements are used to read samples from respective files.

my_array[0] points to FL, now i want to point to 1st sample (considering the files from which inputs are read consists of sample values) of FL.

How do i obtain that using pointers?

Please Help.
Dec 2 '15 #5

Expert Mod 5K+
P: 9,197
You have:

Expand|Select|Wrap|Line Numbers
  1. int32 MIXER_IN_PNTR[6] ={0};
and also:
Expand|Select|Wrap|Line Numbers
  1. int32 *MIXER_IN_PNTR[6];
You aren't allowed to have 2 arrays with the same name.

Which of these is correct?
Dec 2 '15 #6

P: 4
int32 MIXER_IN_PNTR[6] = {0}; is correct
Dec 3 '15 #7

Expert 100+
P: 2,398
MIXER_IN_PNTR is defined as an array of integers, not an array of pointers.

To make it an array of pointers:
int32 *MIXER_IN_PNTR[6];

To initialize it to point at the previously defined integers:
int32 *MIXER_IN_PNTR[6] = { &MIXER_MIXERFL_InPtr, ... };
In this case, you can change the integers pointed to by array elements and you can also change the array elements to point at different integers.

To initialize it so that you can still change the integers pointed to by array elements but can't change the array elements themselves:
int32 * const MIXER_IN_PNTR[6] = { &MIXER_MIXERFL_InPtr, ... };
Dec 3 '15 #8

Post your reply

Sign in to post your reply or Sign up for a free account.