GDB错过了显示const限定符?

时间:2016-08-01 09:32:08

标签: c gcc gdb const pointer-to-array

提供以下来源(main.c):

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

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

  foo(&a);
}

...使用GCC编译(gcc(Debian 4.9.2-10)4.9.2)并在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

...为什么GDB会向我显示(char (*)[4])而不是(const char (*)[4])作为foo()参数pa的类型? const资格赛发生了什么?还是我错过了必不可少的东西? :-S

更新

pa表现得与预期一致。例如,如果做

   char (*t)[4] = pa;

foo()内编译器抱怨:

 warning: initialization from incompatible pointer type

而不是

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

工作正常。

0 个答案:

没有答案