返回信息流程序如下:
int getstring(unsigned char *buf) {
scanf("%s",&buf);
.........................
}
结果每次运行到 scanf("%s",&buf);这里时就报错 错误如下:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb7dc56b0 (LWP 7671)]
0xb7e2efe8 in _IO_vfscanf () from /lib/tls/i686/cmov/libc.so.6
(gdb) bt
#0 0xb7e2efe8 in _IO_vfscanf () from /lib/tls/i686/cmov/libc.so.6
#1 0xb7e36d2b in scanf () from /lib/tls/i686/cmov/libc.so.6
请大家帮忙看一下
谢谢拉!
这是一条镜像帖。来源:北邮人论坛 / cpp / #34564同步于 2009/12/31
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
为什么没法读入键盘输入的字符串??
tomharold
2009/12/31镜像同步12 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
buf 是unsigned char *,指针,里面存的就是地址,不需要再取地址了
【 在 tomharold 的大作中提到: 】
: 程序如下:
: int getstring(unsigned char *buf) {
: scanf("%s",&buf);
: ...................
实在不好意思 之前那个结果我给粘贴错了
实际情况是这样:如果用scanf("%s",&buf);
运行到这一句时错误如下:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb7da76b0 (LWP 7868)]
0x00000000 in ?? ()
如果用scanf("%s",buf);
则错误就如原帖所示:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb7dc56b0 (LWP 7671)]
0xb7e2efe8 in _IO_vfscanf () from /lib/tls/i686/cmov/libc.so.6
(gdb) bt
#0 0xb7e2efe8 in _IO_vfscanf () from /lib/tls/i686/cmov/libc.so.6
#1 0xb7e36d2b in scanf () from /lib/tls/i686/cmov/libc.so.6
这个是一个函数 主程序中定义了一个char *bufout
然后调用getstring(bufout)来从终端读一个字符串进来 如下:
int main(void) {
char *bufout;
...............
getstring(bufout);
...............
}
然后就出错了
【 在 jokerlee 的大作中提到: 】
: &不是关键吧,关键是buf指向哪里有没有分配内存
那么有没有类似、看起来像bufout = malloc(xxxx)一类的语句呢?
【 在 tomharold (Tomharold) 的大作中提到: 】
: 这个是一个函数 主程序中定义了一个char *bufout
: 然后调用getstring(bufout)来从终端读一个字符串进来 如下:
: int main(void) {
: ...................
有的 在主程序里有:
bufout=(char *)malloc(10*sizeof(char));
【 在 FadeToBlack 的大作中提到: 】
: 那么有没有类似、看起来像bufout = malloc(xxxx)一类的语句呢?
【 在 tomharold 的大作中提到: 】
: 有的 在主程序里有:
: bufout=(char *)malloc(10*sizeof(char));
那读入的字符串长度大于10呢?