3

Give the following source (main.c):

void foo(const char (*pa)[4])
{
}

int main(void)
{
  const char a[4] = "bar";

  foo(&a);
}

... compiled with GCC (gcc (Debian 4.9.2-10) 4.9.2) and run under GDB (GNU gdb (Debian 7.7.1+dfsg-5) 7.7.1) ...

(gdb) b main
Breakpoint 1 at 0x4004c8: file main.c, line 7.
(gdb) b foo
Breakpoint 2 at 0x4004be: file main.c, line 3.
(gdb) r
Breakpoint 1, main () at main.c:7
7     const char a[4] = "bar";
(gdb) p &a
$1 = (const char (*)[4]) 0x7fffffffe1a0
(gdb) c
Continuing.

Breakpoint 2, foo (pa=0x7fffffffe1a0) at main.c:3
3   }
(gdb) p pa
$2 = (char (*)[4]) 0x7fffffffe1a0

... why does GDB show me (char (*)[4]) instead of (const char (*)[4]) as type for foo()'s parameter pa? What happened to the const qualifier? Or am I missing something essential? :-S


Update:

pa behaves as expected. If for example doing

   char (*t)[4] = pa;

inside foo() the compiler complains:

 warning: initialization from incompatible pointer type

Whereas doing

   const char (*t)[4] = pa;

works fine.

alk
  • 69,737
  • 10
  • 105
  • 255

0 Answers0