+ 1

question regarding pointer

int a[20]; int *p; p=&a[]; or p=&a;. or anything else for(int i=0;i<5;i++) cout<<p; how to display address of array using pointer

20th Mar 2019, 1:28 PM
Vishesh Saxena
Vishesh Saxena - avatar
4 Respostas
+ 7
Array values are stored at consecutive memory addresses, i.e., one by one. So to display an array using pointer use can add the index to the current pointer address. Like to display a[2], you can use *(p + 2). So your correct code will be this: int a[20]; int[] *p; p=&a; for(int i=0;i<5;i++) cout<< *(p + i);
20th Mar 2019, 1:37 PM
Letsintegreat
Letsintegreat - avatar
+ 5
Vishesh Saxena Sorry I missed something. Datatype of your pointer should int[]* Now fixed by the edit
20th Mar 2019, 1:46 PM
Letsintegreat
Letsintegreat - avatar
+ 1
thanks Zlytherin , but when i did p=&a it shows an error cannot convert 'int (*)[20] ' to 'int*' in assignment
20th Mar 2019, 1:43 PM
Vishesh Saxena
Vishesh Saxena - avatar