【发布时间】:2012-10-27 11:59:20
【问题描述】:
当 dyn_mat 的参数是常量时,代码运行没有任何错误,并且 s1 和 s2 确实存储了输入值。
#include<stdio.h>
int main(int argc, char const *argv[])
{
char *s1, *s2;
int n1=7, n2=8;
printf("Enter, %d \n", n1);
scanf("%s", s1);
scanf("%s", s2);
int dyn_mat[155][347];
return 0;
}
但是使用参数作为变量,比如 n1 和 n2,scanf 读取 s1 会导致分段错误。
【问题讨论】:
-
C 不允许这样做。您的代码具有未定义的行为。
-
"当
dyn_mat的[维度] 是常量时,代码运行没有任何错误,s1和s2确实存储输入值。但是将参数作为变量,比如 n1和 n2,scanf 读取 s1 给出分段错误。”代码在这两种情况下运行时都会出现同样严重的错误,只是碰巧看不到它的影响。
标签: c pointers segmentation-fault