2010-05-12 77 views
4

所以在组装我声明如下字符串:大会GDB打印字符串

Sample db "This is a sample string",0 

在GDB I型 “P样品。”(不含引号),它吐出0x73696854。我想要打印出实际的字符串。所以我尝试了“printf”%s“Sample”(再次没有引号),并且吐出了“无法访问地址0x73696854处的内存”。

简版: 如何在GDB中打印字符串?

回答

8

我的老师刚给我发了邮件。对于任何想知道的人:

p(char[20]) Sample 

其中20是要打印的字符数。

要打印C风格NUL封端的字符串,你也应该能够做到这一点:

print (char*) &Sample 
printf "%s", &Sample 
5

我有同样的问题!试试这个:

x/s &Sample # prints the whole string 

“x” - 通常用于检查数据。

对于一个劲儿地,你可以忧色这个代码

x/c &Sample # prints: "T" 

如果你想看到多个字符,你可以给希望的字符数

x/3c &Sample # prints: "T" "h" "i"