I want to pass a pointer to an array,
Taking your requirement literally, you do it like this:
void test(size_t len, struct Point (*a)[len]) { size_t i; printf("a = %p\n", (void *) a); for (i = 0; i < len; ++i) printf("%f\n", (*a)[i].x); }
And name it as follows:
size_t len = 4; struct Point * p = malloc(len * sizeof *p); for (i = 0; i < len; ++i) { p[i].x = i; printf("%f\n", p[i].x); } printf("p = %p\n", (void *) p); printf("&p = %p\n", (void *) &p); test(len, &p);
You can also implement the same functionality (loop through array elements) by choosing the method suggested by Sourav Ghosh answer . Then you pass the pointer to the array 1 st but the pointer to the array itself.
alk
source share