+ 4
How to pass array to the pointer
C language
4 ответов
+ 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;
}
+ 2
void main()
{
int a[5]={1,2,3,4,5};
int *p;
p=&a[0];
}
+ 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.