[问题]scanf 段错误

软件和网站开发以及相关技术探讨
回复
老怪
帖子: 13
注册时间: 2006-10-14 12:49

[问题]scanf 段错误

#1

帖子 老怪 » 2007-06-24 22:51

如题,哪位能给解释下吗?

我在C程序里调用scanf();

char * s;
. . . .
scanf("%s",s);
. . . .

一到这儿,就会有段错误出来。
老怪
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#2

帖子 eexpress » 2007-06-24 23:04

没分配地址吧。一个指针而已。
● 鸣学
头像
jiangpeng
帖子: 223
注册时间: 2006-07-25 9:33
联系:

#3

帖子 jiangpeng » 2007-06-25 0:28

:lol:不知道...里面有没有malloc哟
Take what man makes and use it, But do not worship it, For it shall pass. -- Anonymous

Twitter @jiangpeng
老怪
帖子: 13
注册时间: 2006-10-14 12:49

#4

帖子 老怪 » 2007-06-25 8:22

没有malloc,只是调用了scanf()
今天又有这种错误出现了。
看来是程序中指针有问题。代码如下:

代码: 全选

 #include<stdio.h>
  5
  6 int** func(){
  7   int i = 0;
  8   int j = 0;
  9   int sum = 0;
 10   static int **a;
 11   int index1 = 0;
 12   int index2 = 0;
 13   for(i = 1;i <= 3;i ++){
 14     a[index1][index2] = i;
 15     for(j = 1;j <= 5;j ++){
 16       a[index1][index2] = j;
 17       a[index1][index2] = 8 - i - j;
 18       sum ++;
 19       a[index1][index2] = sum;
 20       index1 ++;
 21       index2 = 0;
 22     }
 23     a[index1][0] = -1;/*用于标记数组是否结束*/
 24   }
 25   return a;
 26 }
 27
 28 /* main */
 29 int main(){
 30   int **a;
 31   int index = 0;
 32   a = func();
 33   while(a[index][0] != -1){
 34     printf("%3d%3d%3d",a[index][0],a[index][1],a[index][2]);
 35     index ++;
 36   }
 37 }
期待解决!
老怪
老怪
帖子: 13
注册时间: 2006-10-14 12:49

#5

帖子 老怪 » 2007-06-25 8:57

好像是解决了
我在网上查了个文章。
另发了个帖子。
老怪
回复