How can I declare an array of pointers.. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

How can I declare an array of pointers..

I want to create two arrays of same length one which can store number another which can store its pointers..

30th Sep 2017, 3:58 AM
Omkar Tripathi
Omkar Tripathi - avatar
6 Answers
+ 11
To create an array of pointers, the following syntax may be used: type* array_of_pointers [size_t size]; Eg - int* arr[6]; // An array of 6 int pointers, each having a capacity to store a reference of an element, or an int array...
30th Sep 2017, 4:04 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 5
@Kinshuk Vasisht (+1) I say, that's an array of pointers! </declaration>
30th Sep 2017, 4:26 AM
Kirk Schafer
Kirk Schafer - avatar
+ 5
Thanks.. @ Kinshuk Vasisht..
30th Sep 2017, 5:23 AM
Omkar Tripathi
Omkar Tripathi - avatar
+ 4
but how will I associate that array of pointers... with my array of integer.. will this be like.. int a[6]; int *p[6]; *p[6]=&a[6];
30th Sep 2017, 4:34 AM
Omkar Tripathi
Omkar Tripathi - avatar
+ 4
@Aryan Sharma Use a for loop, in the following way : for(size_t i = 0; i < size; i++) { arr[i] = &a[i]; } Thats it. To use, dereference in the following way : cout << *(arr[3]) << endl; // Print 4th element of a.
30th Sep 2017, 5:19 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
I think this code should resolve your query... https://code.sololearn.com/cBf3xrkk8j0q/?ref=app
30th Sep 2017, 7:56 AM
Sagar Sonavane