Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C function that returns a pointer to an array correct syntax?

In C you can declare a variable that points to an array like this:

int int_arr[4] = {1,2,3,4};
int (*ptr_to_arr)[4] = &int_arr;

Although practically it is the same as just declaring a pointer to int:

int *ptr_to_arr2 = int_arr;

But syntactically it is something different.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Now, how would a function look like, that returns such a pointer to an array (of int e.g.) ?

>Solution :

A declaration of int is int foo;.

A declaration of an array of 4 int is int foo[4];.

A declaration of a pointer to an array of 4 int is int (*foo)[4];.

A declaration of a function returning a pointer to an array of 4 int is int (*foo())[4];. The () may be filled in with parameter declarations.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading