0

I was having a technical test, and they had one question which confused me a bit.

This is the exact question:

image

I know it may be a basic question, but I also would love if someone would help me with a sequence which I can use to understand pointers nomenclature for future use.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 6
    https://cdecl.org describes `char (*p(int, int))[10]` as: `declare p as function (int, int) returning pointer to array 10 of char`. Read up about the [spiral rule](http://c-faq.com/decl/spiral.anderson.html), but be aware of its [gotchas](https://stackoverflow.com/questions/16260417/) – Remy Lebeau May 04 '21 at 22:23
  • Tricky one. Little shame in stumbling on it. – user4581301 May 04 '21 at 22:27
  • 3
    Actually it should be "None of the above", since the code declares 'p' but the question asks for 'P'. C++ is case sensitive after all. – KorbenDose May 04 '21 at 22:28
  • @RemyLebeau Can C/C++ function really return an array instead of pointer? Looks more like option 3 to me. – Semyon Burov May 04 '21 at 22:30
  • 1
    @SemyonBurov a function cannot return a C-style array by value, no. It can return a C++-style `std::array` by value, though. And a function can certainly return a pointer to a C/C++ array. And this code in question is indeed declaring a function that returns a pointer to a C-style `char[10]` array. – Remy Lebeau May 04 '21 at 22:32
  • @RemyLebeau I used incorrect terminology and called "pointer to static array of 10" simply "array". Does it really have any meaning to return "pointer to static array of 10"? If it was defined in scope of function, the pointer value will be returned, but the memory will be deallocated, so any attempt to access it via pointer will result in Access Violation exception. – Semyon Burov May 04 '21 at 22:55
  • @SemyonBurov "*Does it really have any meaning to return "pointer to static array of 10"?*" - sure, there are use-cases where that is valid. Such as returning a pointer to a class member, or to a global/singleton. "*If it was defined in scope of function, the pointer value will be returned, but the memory will be deallocated*" - yes, returning a dangling pointer to a local variable that went out of scope would be invalid. "*so any attempt to access it via pointer will result in Access Violation exception*" - technically, it is just **undefined behavior**, an AV is not guaranteed. – Remy Lebeau May 04 '21 at 23:06
  • Thank you all for the help, I now get it. This [link](https://www.codeproject.com/Articles/7042/How-to-interpret-complex-C-C-declarations) has helped me alot, too. I got it from a similar question. Thanks for the help :D – Amr Mohamed May 05 '21 at 09:39

0 Answers0