0

I read this question: what does the line int *(*(x[3])())[5]; do in C?

which have the code line:

int *(*(*x[3])())[5];

in this answer https://stackoverflow.com/a/37364752/4386427

According to http://cdecl.org/ this means

declare x as array 3 of pointer to function returning pointer to array 5 of pointer to int

Now I'm wondering about this part:

function returning pointer to array 5 of pointer to int

How does the proto-type for a function returning pointer to array 5 of pointer to int look?

I tried this:

int* g()[5]    <---- ERROR: 'g' declared as function returning an array
{
    int** t = NULL;
    // t = malloc-stuff
    return t;
}

which doesn't compile.

Then I tried

#include <stdio.h>
#include <stdlib.h>

int *(*(*x[3])())[5];

int** g()
{
    int** t = NULL;
    // t = malloc-stuff
    return t;
}

int main(void) {
    x[0] = g;
    return 0;
}

which compiles fine but now the return type is more like pointer to pointer to int. There is nothing that says pointer to array 5 of pointer to int

So my question is:

Is it at all possible to write a function which returns pointer to array 5 of pointer to int ?

If yes, how does the proto-type look?

If no, what is the purpose of 5 in the declaration of x?

int *(*(*x[3])())[5];
                  ^
                what does 5 mean here?
5
  • 2
    troll? meta.stackoverflow.com/questions/313324/… Commented May 21, 2016 at 16:21
  • @NiallCosgrove - what do you mean? Please elaborate. Commented May 21, 2016 at 16:22
  • @NiallCosgrove - It is indeed a genuine question. Commented May 21, 2016 at 16:27
  • 2
    int* (*g())[5] More parentheses are clearly the universal antidote, as more indirection is a panacea. Commented May 21, 2016 at 16:29
  • @EOF - Great - it compiles fine with your suggestion. Still, I'm puzzled as I can assign x[1]=g2; even when int* (*g())[2] (i.e. 3 instead of 5). I don't get any warnings - at least not here: ideone.com/mVt8g9 Commented May 21, 2016 at 16:46

1 Answer 1

1

an array[5] of ints would be:

int array[5]; /* read: array of 5 ints */

and a pointer to that array (not just to the first element of it, but the whole array of 5!) would be:

int(* ptr)[5] = &array; /* read: pointer to an array of 5 ints */

and a function returning such a pointer would be:

int(* g())[5]; /*read: a function returning a pointer to an array of 5 ints */

Following the same logic, an array[5] of pointers_to_int would be:

int* array_of_ptrs[5]; /* read: array of 5 pointers_to_int */

and a pointer to that array would be:

int* (* PTR)[5] = &array_of_ptrs; /* read: pointer to an array of 5 pointers_to_int */

and theeeen a function returning such a pointer would be:

int* (* g())[5] /* read: function returning a pointer to an array of 5 pointers_to_int*/
/* just like @EOF said in the comments above! */

Let's try it out:

#include <stdio.h>

int array[5] ={1, 2, 3, 4, 5};

int a = 6, b = 7, c = 8, d = 9, e = 10;
int* array_of_ptrs[5] = {&a, &b, &c, &d, &e};

int(* g())[5] 
{
    return &array;
}

int* (* gg())[5]
{
    return &array_of_ptrs; 
}

int main()
{
int(* ptr)[5]; 
ptr = g();

int* (* PTR)[5];     
PTR = gg();
    
printf
("the value of the dereferenced 1st element of array_of_ptrs is: %d", *(*PTR)[0]);  

return 0;
}

clang prog.c -Wall -Wextra -std=gnu89 output:

the value of the dereferenced 1st element of array_of_ptrs is: 6

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.