这是我输入的一个程序(逐字逐句,写字母,特殊字符的特殊字符)来自C编程。但是我的编译器没有编译它(Xcode 4.6.3,Terminal 2.2.3) )。
以下是我收到的程序及以下错误消息。我不明白为什么我会收到这些错误消息。
// program to multiply two-dimensional matrix by a scalar
#include <stdio.h>
void scalar_multiply (int num_rows, int num_columns, int matrix[num_rows][num_columns], int scalar);
void display_matrix (int num_rows, int num_columns, matrix[num_rows][num_columns]);
int main(void)
{
int sample_matrix[3][5] =
{
{ 7, 16, 55, 13, 12},
{12, 10, 52, 0, 7},
{-2, 1, 2, 4, 9}
};
printf("Original matrix:\n");
display_matrix(3, 5, sample_matrix);
scalar_multiply(3, 5, sample_matrix, 2);
printf("\nMultiplied by 2:\n");
display_matrix(3, 5, sample_matrix);
scalar_multiply(3, 5, sample_matrix, -1);
printf("\nMultiplied by -1:\n");
display_matrix(3, 5, sample_matrix);
return 0;
}
// function to multiply a matrix by a scalar
void scalar_multiply (int num_rows, int num_columns, int matrix[num_rows][num_columns], int scalar)
{
int row, column;
for (row = 0; row < num_rows; row++)
for (column = 0; column < num_columns; column++)
matrix[row][column] *= scalar;
}
void display_matrix (int num_rows, int num_columns, matrix[num_rows][num_columns])
{
int row, column;
for (row = 0; row < num_rows; row++)
{
for (column = 0; column < num_columns; column++)
printf("%5i", matrix[row][column]);
printf("\n");
}
}
错误讯息:
prog7-14.c:6: error: expected declaration specifiers or ‘...’ before ‘matrix’
prog7-14.c: In function ‘main’:
prog7-14.c:18: error: too many arguments to function ‘display_matrix’
prog7-14.c:23: error: too many arguments to function ‘display_matrix’
prog7-14.c:28: error: too many arguments to function ‘display_matrix’
prog7-14.c: At top level:
prog7-14.c:42: error: expected declaration specifiers or ‘...’ before ‘matrix’
prog7-14.c: In function ‘display_matrix’:
prog7-14.c:49: error: ‘matrix’ undeclared (first use in this function)
prog7-14.c:49: error: (Each undeclared identifier is reported only once
prog7-14.c:49: error: for each function it appears in.)
Allas-MacBook-Pro:ch7 Alla$ touch prog7-14.c
Allas-MacBook-Pro:ch7 Alla$ gcc prog7-14.c -o prog7-14
prog7-14.c:6: error: expected declaration specifiers or ‘...’ before ‘matrix’
prog7-14.c: In function ‘main’:
prog7-14.c:18: error: too many arguments to function ‘display_matrix’
prog7-14.c:23: error: too many arguments to function ‘display_matrix’
prog7-14.c:28: error: too many arguments to function ‘display_matrix’
prog7-14.c: At top level:
prog7-14.c:42: error: expected declaration specifiers or ‘...’ before ‘matrix’
prog7-14.c: In function ‘display_matrix’:
prog7-14.c:49: error: ‘matrix’ undeclared (first use in this function)
prog7-14.c:49: error: (Each undeclared identifier is reported only once
prog7-14.c:49: error: for each function it appears in.)
谢谢!
答案 0 :(得分:3)
List<String> favoredNodes
必须是
void display_matrix (int num_rows, int num_columns, matrix[num_rows][num_columns]);
您在void display_matrix (int num_rows, int num_columns, int matrix[num_rows][num_columns]);
int
答案 1 :(得分:1)
在声明原型和定义 display_matrix
函数时,您错过了在int
之前键入matrix[num_rows][num_columns]
。
这会产生错误: display_matrix
的参数太多。
对display_matrix
原型进行更改。
void display_matrix (int num_rows, int num_columns, int matrix[num_rows][num_columns]);
将类似的更改更新为display_matrix
定义。
void display_matrix (int num_rows, int num_columns, int matrix[num_rows][num_columns])
{
int row, column;
.... rest of the code
}