Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you declare a pointer to a function that returns a pointer to an array of int values in C / C++?

Is this correct?

int (*(*ptr)())[];

I know this is trivial, but I was looking at an old test about these kind of constructs, and this particular combination wasn't on the test and it's really driving me crazy; I just have to make sure. Is there a clear and solid understandable rule to these kind of declarations? (ie: pointer to... array of.. pointers to... functions that.... etc etc) Thanks!

R

like image 583
Russel Avatar asked Aug 05 '10 02:08

Russel


People also ask

How do you return a pointer to an array from a function in C?

C programming does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.

How do you declare a pointer to an array of pointers to?

p = arr; // Points to the whole array arr. p: is pointer to 0th element of the array arr, while ptr is a pointer that points to the whole array arr. The base type of p is int while base type of ptr is 'an array of 5 integers'.

How do you declare a pointer to a function?

int foo(int); Here foo is a function that returns int and takes one argument of int type. So as a logical guy will think, by putting a * operator between int and foo(int) should create a pointer to a function i.e. int * foo(int);

Which of the following declares a pointer to a function that returns a pointer to an integer?

int (*f) (int * ): A pointer to a function that takes an integer pointer as argument and returns an integer.


2 Answers

The right-left rule makes it easy.

int (*(*ptr)())[];can be interpreted as

Start from the variable name ------------------------------- ptr

Nothing to right but ) so go left to find * -------------- is a pointer

Jump out of parentheses and encounter () ----------- to a function that takes no arguments(in case of C unspecified number of arguments)

Go left, find * ------------------------------------------------ and returns a pointer

Jump put of parentheses, go right and hit [] ---------- to an array of

Go left again, find int ------------------------------------- ints.

like image 135
Prasoon Saurav Avatar answered Oct 08 '22 20:10

Prasoon Saurav


In almost all situations where you want to return a pointer to an array the simplest thing to do is to return a pointer to the first element of the array. This pointer can be used in the same contexts as an array name an provides no more or less indirection than returning a pointer of type "pointer to array", indeed it will hold the same pointer value.

If you follow this you want a pointer to a function returning a pointer to an int. You can build this up (construction of declarations is easier than parsing).

Pointer to int:

int *A;

Function returning pointer to int:

int *fn();

pointer to function returning a pointer to int:

int *(*pfn)();

If you really want to return a pointer to a function returning a pointer to an array of int you can follow the same process.

Array of int:

int A[];

Pointer to array of int:

int (*p)[];

Function returning pointer ... :

int (*fn())[];

Pointer to fn ... :

int (*(*pfn)())[];

which is what you have.

like image 36
CB Bailey Avatar answered Oct 08 '22 22:10

CB Bailey