Why does the following code give me a segmentation fault?
为什么以下代码会给我一个分段错误?
#define MAXROWS 10
#define MAXCOLS 10
void getInput (int *data[MAXROWS][MAXCOLS]) {
int rows, cols;
int curRow, curCol;
printf ("How many rows and cols?");
scanf ("%d %d", rows, cols);
for (curRow = 0; curRow < rows; curRow++) {
for (curCol = 0; curCol < cols; curCol++) {
scanf ("%d", data[curRow][curCol]);
printf ("%d\n", *data[curRow][curCol]);
}
}
}
void main () {
int data[MAXROWS][MAXCOLS];
getInput (data);
}
It seems to be that the scanf
and printf
statements aren't getting the right data type passed in, but I can't work out what they should be.
似乎scanf和printf语句没有传入正确的数据类型,但我无法弄清楚它们应该是什么。
How can I change it so that it works properly?
如何更改它以使其正常工作?
3 个解决方案
#1
4
This declares an array of MAXROWS
arrays of pointers to int
.
这声明了一个指向int的MAXROWS指针数组。
int *data[MAXROWS][MAXCOLS];
However, in a function definition, top level arrays (of any size) are equivalent to pointers because arrays always decay to pointers to the type of the array member on passing to a function.
但是,在函数定义中,*数组(任何大小)都等效于指针,因为数组在传递给函数时总是衰减到指向数组成员类型的指针。
So your function definition is equivalent to:
所以你的函数定义相当于:
void getInput (int *(*data)[MAXCOLS])
i.e. a pointer to an array of MAXCOLS
pointers to int
.
即指向指向int的MAXCOLS指针数组的指针。
As your code stands, you never initialize any of the int
pointers in the array, as you are passing a 2d array of int
s as a pointer to a 2d array of int *
.
正如你的代码所代表的那样,你永远不会初始化数组中的任何int指针,因为你传递了一个2d数组的int作为指向int *的2d数组的指针。
What you probably want to pass, is a pointer to an array of MAXCOLS
int
:
您可能想要传递的是指向MAXCOLS int数组的指针:
void getInput (int (*data)[MAXCOLS])
or equivalently:
void getInput (int data[][MAXCOLS])
Then you do the following:
然后执行以下操作:
int main(void)
{
int data[MAXROWS][MAXCOLS];
getInput(data);
return 0;
}
You are then passing your 2d array as a pointer to its first element (a pointer to a row or an array of MAXCOLS
int
s).
然后,您将2d数组作为指向其第一个元素的指针(指向行或MAXCOLS整数数组的指针)。
If you make sure change be sure to change:
如果您确保更改,请务必更改:
scanf ("%d", data[curRow][curCol]);
printf ("%d\n", *data[curRow][curCol]);
to:
scanf ("%d", &data[curRow][curCol]);
printf ("%d\n", data[curRow][curCol]);
Also, check your parameters here:
另外,请在此处检查您的参数:
scanf ("%d %d", &rows, &cols);
You need to be passing pointers to rows
and cols
.
您需要将指针传递给行和列。
Make sure to add some bounds checking to your input function so that you don't attempt to read more rows and columns than MAXROWS
or MAXCOLS
.
确保在输入函数中添加一些边界检查,这样您就不会尝试读取比MAXROWS或MAXCOLS更多的行和列。
#2
0
scanf accepts address of variables, not the content of it:
scanf接受变量的地址,而不是它的内容:
void getInput (int data[][MAXCOLS]) {
int rows, cols;
int curRow, curCol;
printf ("How many rows and cols?");
scanf ("%d %d", &rows, &cols);
//scanf ("%d %d", &rows, &cols);
for (curRow = 0; curRow < rows; curRow++) {
for (curCol = 0; curCol < cols; curCol++) {
scanf ("%d", &data[curRow][curCol]);
printf ("%d\n", data[curRow][curCol]);
}
}
}
#3
0
There were a few different problems.
有几个不同的问题。
First, when passing arrays to functions you only need the definition of N-1 dimensions. For example if you're passing a 3D array you would put the size of the last 2 dimensions in the function sig and leave the first one empty.
首先,将数组传递给函数时,只需要定义N-1维。例如,如果您传递3D数组,则将最后2个维度的大小放在函数sig中,并将第一个维度留空。
foo(int threeD[][10][15]);
Second, scanf takes the address of the argument, which for your array looks like this
其次,scanf获取参数的地址,对于您的数组看起来像这样
&data[curRow][curCol]
Third, you should always check the range of you input to make sure it's valid:
第三,你应该经常检查你输入的范围,以确保它是有效的:
if (rows > MAXROWS || cols > MAXCOLS) {
printf("Bad array dimensions\n");
return;
}
Fourth, always compile with all warnings turned on - the compiler will warn you about allot of these things:
第四,总是在打开所有警告的情况下编译 - 编译器会警告你有关这些事情:
gcc -Wall pass_array.c -o pass_array
.
#include <stdio.h>
#define MAXROWS 10
#define MAXCOLS 10
void getInput (int data[][MAXCOLS]) {
int rows, cols;
int curRow, curCol;
printf ("How many rows and cols?");
scanf ("%d %d", &rows, &cols);
if (rows > MAXROWS || cols > MAXCOLS) {
printf("Bad array dimensions\n");
return;
}
for (curRow = 0; curRow < rows; curRow++) {
for (curCol = 0; curCol < cols; curCol++) {
scanf ("%d", &data[curRow][curCol]);
printf ("%d\n", data[curRow][curCol]);
}
}
}
int main () {
int data[MAXROWS][MAXCOLS];
getInput (data);
return 0;
}
#1
4
This declares an array of MAXROWS
arrays of pointers to int
.
这声明了一个指向int的MAXROWS指针数组。
int *data[MAXROWS][MAXCOLS];
However, in a function definition, top level arrays (of any size) are equivalent to pointers because arrays always decay to pointers to the type of the array member on passing to a function.
但是,在函数定义中,*数组(任何大小)都等效于指针,因为数组在传递给函数时总是衰减到指向数组成员类型的指针。
So your function definition is equivalent to:
所以你的函数定义相当于:
void getInput (int *(*data)[MAXCOLS])
i.e. a pointer to an array of MAXCOLS
pointers to int
.
即指向指向int的MAXCOLS指针数组的指针。
As your code stands, you never initialize any of the int
pointers in the array, as you are passing a 2d array of int
s as a pointer to a 2d array of int *
.
正如你的代码所代表的那样,你永远不会初始化数组中的任何int指针,因为你传递了一个2d数组的int作为指向int *的2d数组的指针。
What you probably want to pass, is a pointer to an array of MAXCOLS
int
:
您可能想要传递的是指向MAXCOLS int数组的指针:
void getInput (int (*data)[MAXCOLS])
or equivalently:
void getInput (int data[][MAXCOLS])
Then you do the following:
然后执行以下操作:
int main(void)
{
int data[MAXROWS][MAXCOLS];
getInput(data);
return 0;
}
You are then passing your 2d array as a pointer to its first element (a pointer to a row or an array of MAXCOLS
int
s).
然后,您将2d数组作为指向其第一个元素的指针(指向行或MAXCOLS整数数组的指针)。
If you make sure change be sure to change:
如果您确保更改,请务必更改:
scanf ("%d", data[curRow][curCol]);
printf ("%d\n", *data[curRow][curCol]);
to:
scanf ("%d", &data[curRow][curCol]);
printf ("%d\n", data[curRow][curCol]);
Also, check your parameters here:
另外,请在此处检查您的参数:
scanf ("%d %d", &rows, &cols);
You need to be passing pointers to rows
and cols
.
您需要将指针传递给行和列。
Make sure to add some bounds checking to your input function so that you don't attempt to read more rows and columns than MAXROWS
or MAXCOLS
.
确保在输入函数中添加一些边界检查,这样您就不会尝试读取比MAXROWS或MAXCOLS更多的行和列。
#2
0
scanf accepts address of variables, not the content of it:
scanf接受变量的地址,而不是它的内容:
void getInput (int data[][MAXCOLS]) {
int rows, cols;
int curRow, curCol;
printf ("How many rows and cols?");
scanf ("%d %d", &rows, &cols);
//scanf ("%d %d", &rows, &cols);
for (curRow = 0; curRow < rows; curRow++) {
for (curCol = 0; curCol < cols; curCol++) {
scanf ("%d", &data[curRow][curCol]);
printf ("%d\n", data[curRow][curCol]);
}
}
}
#3
0
There were a few different problems.
有几个不同的问题。
First, when passing arrays to functions you only need the definition of N-1 dimensions. For example if you're passing a 3D array you would put the size of the last 2 dimensions in the function sig and leave the first one empty.
首先,将数组传递给函数时,只需要定义N-1维。例如,如果您传递3D数组,则将最后2个维度的大小放在函数sig中,并将第一个维度留空。
foo(int threeD[][10][15]);
Second, scanf takes the address of the argument, which for your array looks like this
其次,scanf获取参数的地址,对于您的数组看起来像这样
&data[curRow][curCol]
Third, you should always check the range of you input to make sure it's valid:
第三,你应该经常检查你输入的范围,以确保它是有效的:
if (rows > MAXROWS || cols > MAXCOLS) {
printf("Bad array dimensions\n");
return;
}
Fourth, always compile with all warnings turned on - the compiler will warn you about allot of these things:
第四,总是在打开所有警告的情况下编译 - 编译器会警告你有关这些事情:
gcc -Wall pass_array.c -o pass_array
.
#include <stdio.h>
#define MAXROWS 10
#define MAXCOLS 10
void getInput (int data[][MAXCOLS]) {
int rows, cols;
int curRow, curCol;
printf ("How many rows and cols?");
scanf ("%d %d", &rows, &cols);
if (rows > MAXROWS || cols > MAXCOLS) {
printf("Bad array dimensions\n");
return;
}
for (curRow = 0; curRow < rows; curRow++) {
for (curCol = 0; curCol < cols; curCol++) {
scanf ("%d", &data[curRow][curCol]);
printf ("%d\n", data[curRow][curCol]);
}
}
}
int main () {
int data[MAXROWS][MAXCOLS];
getInput (data);
return 0;
}