2

I've recently discovered the syntax for pointers to fixed arrays. While experimenting with it, I was surprised to see that pointer decay didn't seem to work as in the below example:

#include <iostream>

int main( int argc, char* argv[] )
{
  char pa[3];
  //char (*parr)[3] = &pa; // This works.
  char (*parr)[3] = pa;    // This doesn't work - why?
  std::cout << (void*)pa << std::endl;
  std::cout << (void*)parr << std::endl;
  return 0;
}

Output:

prog.cpp: In function 'int main(int, char**)':
prog.cpp:10:21: error: cannot convert 'char*' to 'char (*)[3]' in initialization
char (*parr)[3] = pa;
       ^

Compilation performed with gcc-4.9.2 (Code Chef).

Can someone please help identify what is wrong here?

Update:

This is an interesting but even more puzzling finding: the pointer decay works as expected if compiling C-style (I selected "C" as the compiler in Code Chef, although it still shows gcc-4.9.2. Perhaps there are different compiler flags being passed to differentiate between C and C++ compilation.)

The C version of the above code:

#include <stdio.h>

int main( int argc, char* argv[] )
{
  char pa[3];
  /* char (*parr)[3] = &pa; */
  char (*parr)[3] = pa;
  printf( "0x%x\n", pa);
  printf( "0x%x\n", parr);
  return 0;
}

Output from the compiled C code:

0xbf92b1fd
0xbf92b1fd
2
  • Why bother with old-style C arrays when modern C++ has std::array and std::vector? Commented May 25, 2017 at 18:42
  • 2
    If you are using c++, consider using std::array instead. Commented May 25, 2017 at 18:42

1 Answer 1

7

The problem with

char (*parr)[3] = pa;

Is that parr is a pointer to an array, which is not the same as a pointer to the first element of an array(which is what an array name can decay into). In order to get it to work you need the address of pa like

char (*parr)[3] = &pa;
Sign up to request clarification or add additional context in comments.

15 Comments

Do you know why the nonworking pointer decay syntax in C++ seems to work in the C-style code? I wouldn't have expected this to be a point of difference between C and C++.
Also, are you saying that &pa[0] is different than &pa? I'd always treated them interchangeably, but I'd also never before used the "pointer to fixed array" syntax before.
In this case &pa[0] is a char* pointing to the first element. &pa is a char (*)[3] pointing to the array. They have the same value but a different type.
@StoneThrow It works in C for you because of a compiler extension. GCC rejects it only if -pedantic-errors is present.
@StoneThrow &pa and &pa[0] are the same address, but the types are different - one points to an array and one points to an element of that array. This is similar to taking the address of a struct/class vs taking the address of the first data member of that struct/class.
|

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.