6

I'm not able to understand this statement of code that I came across during my interview.

int(*(*ptr[3])(char*))[2];

I've tried looking at an IDE but all I have is that it is an array of data type

int (*(*[3])(char *)) 

I wasn't able to understand this.

8
  • 1
    What part of it do you not understand? What have you tried to resolve this? Are you familiar with function pointer declarations? Commented Aug 22, 2019 at 5:54
  • 4
    This tool is often helpful. Commented Aug 22, 2019 at 6:02
  • What you have is wrong. Declarations are read from the inside out, not outside in. Commented Aug 22, 2019 at 6:03
  • 1
    c-faq.com/decl/cdecl1.html Commented Aug 22, 2019 at 6:09
  • @melpomene outside-in works better for me; if you have the type without the name (e.g. int(*(*[3])(char*))[2] here) it may not even be clear where the middle is! I would approach that type by noting that it is of the form int(..........)[2] which I would solve by working out int ........ (via same method recursively) and then replacing int with "array of 2 ints". Commented Aug 22, 2019 at 6:16

2 Answers 2

6

May be you could just break it down one at a time to understand the syntax better. First start up with a simple definition without the array notation

int(*(*ptr)(char*));

So ptr is a function pointer that takes a char pointer as an argument and returns a pointer to an int. Now extending it to the array notation

int(*(*ptr[3])(char*))[2];

which means you have an array of function pointers, each of which will take a char pointer argument and return a pointer to an array of two integers.

You can see this working if you have a make a function call using these pointers you define. Note that, the below functions are for demonstrative purposes only and do not convey any logical purpose

#include <iostream>

static int arr[2] = { 2, 2 };

// initialize  'bar' as a function that accepts char* and returns
// int(*)[2]
int (*bar(char * str))[2] {
    return &arr;
}

int main() {
    // pointer definition, not initialized yet
    int(*(*foo[3])(char*))[2];
    char ch = 'f';
    // as long as the signatures for the function pointer and 
    // bar matches, the assignment below shouldn't be a problem
    foo[0] = bar;
    // invoking the function by de-referencing the pointer at foo[0]
    // Use 'auto' for C++11 or declare ptr as int (*ptr)[2] 
    auto *ptr = (*foo[0])(&ch);
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

2

You have to unfold the type from the inside out and recall that [] and () (on the right) bind stronger than * (on the left). To override this binding, parentheses () are used.

int(*(*ptr[3])(char*))[2];
^   ^ ^^  ^   ^       ^
|   | ||  |   |       |
|   | |ptr is |       |
|   | |   |   |       |
|   | |   an array of three
|   | |       |       |
|   | pointers to     |
|   |         |       |
|   |         a function taking a char* and returning
|   |                 |
|   a pointer to      |
|                     an array of two
ints

i.e. "ptr is an array of three pointers to a function taking a char* and returning a pointer to an array of two ints".

1 Comment

This is like a maze ++

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.