How to pass array to the pointer | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How to pass array to the pointer

C language

5th Jun 2019, 9:59 AM
Deepak
Deepak - avatar
4 Answers
+ 3
If I understood the question you seem to be asking for a pointer to an array, it can be done the following way: int main(void) { int array[5] = { 1, 2, 3, 4, 5 }; int i, (*p)[5]; p = &array; for (i = 0 ; i < 5; i++) printf("%d\n", (*p)[i]); return 0; }
5th Jun 2019, 10:39 AM
Cluck'n'Coder
Cluck'n'Coder - avatar
+ 2
void main() { int a[5]={1,2,3,4,5}; int *p; p=&a[0]; }
6th Jun 2019, 8:25 AM
sree harsha
sree harsha - avatar
+ 1
In C, arrays decay into pointers. int f(int8_t arr[]) { ptintf("%d\n", sizeof(arr); } int main() { int8_t arr[200]; printf("%d\n", sizeof(arr)); f(arr); } This will first print 200 (the size of the array in bytes), followed by 4 or 8 (the size of a pointer on the machine). Because p[i], for a pointer p is the same as p + i, a pointer to an array of type T is just a pointer to T.
5th Jun 2019, 6:01 PM
Vlad Serbu
Vlad Serbu - avatar