C语言 表达式必须有一个指向 C 中对象类型的指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20064650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expression must have a pointer to object type in C
提问by anansharm
I am trying to use pointers in functions and arrays and when I call the report function in main, I keep getting an error Expression must have a pointer to object type. I have tried everything. Nothing seem to be working. Can anyone please let me know what I am doing wrong?
我试图在函数和数组中使用指针,当我在 main 中调用报告函数时,我不断收到错误表达式必须有一个指向对象类型的指针。我已经尝试了一切。似乎没有任何工作。谁能让我知道我做错了什么?
Please note: without the reportfunction, if I call the other functions separately in mainit works. It's not working only with the reportfunction.
请注意:没有该report功能,如果我在main其中单独调用其他功能。它不仅适用于该report功能。
#include <stdio.h>
#include <conio.h>
void print(int *list, int row_count, int column_count);
void rowaverage(int *list, int row_count, int column_count);
void allaverage(int *list, int row_count, int column_count);
void largest(int *list, int row_count, int column_count);
void report(int *list, int row_count, int column_count);
int main()
{
int i = 1, row, column;
int list[3][5];
printf("Enter 3 sets of 5 integers::\n");
for (row = 0; row < 3; row++)
{
printf("Elements in the %d set are ::\n", row);
for (column = 0; column < 5; column++)
{
printf("Element No. %d is ", i++);
scanf("%d", &list[row][column]);
}
printf("\n");
i = 1;
}
printf("The elements in array are:\n");
report(&list[0][0], row, column);
getch();
return 0;
}
void print(int *list, int row_count, int column_count)
{
int column, row;
for (row = 0; row < row_count; row++)
{
for (column = 0; column < column_count; column++)
{
printf("%8d", *(list + row * column_count + column));
}
printf("\n");
}
}
void rowaverage(int *list, int row_count, int column_count)
{
int column, row;
for (row = 0; row < row_count; row++)
{
float sum = 0, count = 0;
for (column = 0; column < column_count; column++)
{
sum += *(list + row * column_count + column);
count++;
}
printf("Average of row %d is %.2f\n", row, (sum / count));
}
}
void allaverage(int *list, int row_count, int column_count)
{
int column, row;
float sum = 0, count = 0;
for (row = 0; row < row_count; row++)
{
for (column = 0; column < column_count; column++)
{
sum += *(list + row * column_count + column);
count++;
}
}
printf("Average of all elements in array is %.2f\n", (sum / count));
}
void largest(int *list, int row_count, int column_count)
{
int column = 0, row = 0;
int largest = *(list + row * column_count + column);
for (row = 0; row < row_count; row++)
{
for (column = 0; column < column_count; column++)
{
if (largest < *(list + row * column_count + column))
{
largest = *(list + row * column_count + column);
}
}
}
printf("The largest number in the array is %d\n", largest);
}
void report(int *list, int row_count, int column_count)
{
int row = 0, column = 0;
print(list[0][0], row, column);
printf("\n");
rowaverage(list[0][0], row, column);
printf("\n");
allaverage(list[0][0], row, column);
printf("\n");
largest(list[0][0], row, column);
}
回答by tyilmaz
In report function remove that line and see below report function:
在报告功能中删除该行并查看以下报告功能:
int row = 0, column = 0;
In functions, use list as
在函数中,使用列表作为
int list[][5]
Call list as
通话清单为
list
not as
不像
list[0][0]
Here is the complete code:
这是完整的代码:
#include <stdio.h>
#include <stdlib.h>
void print(int list[][5], int row_count, int column_count)
{
int column, row;
for (row = 0; row < row_count; row++) {
for (column = 0; column < column_count; column++)
printf("%8d", list[row][column]);
printf("\n");
}
}
void rowaverage(int list[][5], int row_count, int column_count)
{
int column, row;
for (row = 0; row < row_count; row++) {
float sum = 0, count = 0;
for (column = 0; column < column_count; column++) {
sum += list[row][column];
count++;
}
printf("Average of row %d is %.2f\n", row, (sum / count));
}
}
void allaverage(int list[][5], int row_count, int column_count)
{
int column, row;
float sum = 0, count = 0;
for (row = 0; row < row_count; row++) {
for (column = 0; column < column_count; column++) {
sum += list[row][column];
count++;
}
}
printf("Average of all elements in array is %.2f\n", (sum / count));
}
void largest(int list[][5], int row_count, int column_count)
{
int column = 0, row = 0;
int largest = list[0][0];
for (row = 0; row < row_count; row++) {
for (column = 0; column < column_count; column++) {
if (largest < list[row][column]) {
largest = list[row][column];
}
}
}
printf("The largest number in the array is %d\n", largest);
}
void report(int list[][5], int row_count, int column_count)
{
print(list, row_count, column_count);
printf("\n");
rowaverage(list, row_count, column_count);
printf("\n");
allaverage(list, row_count, column_count);
printf("\n");
largest(list, row_count, column_count);
}
int main()
{
int i = 1, row, column;
int list[3][5];
printf("Enter 3 sets of 5 integers::\n");
for (row = 0; row < 3; row++) {
printf("Elements in the %d set are ::\n", row);
for (column = 0; column < 5; column++) {
printf("Element No. %d is ", i++);
scanf("%d", &list[row][column]);
}
printf("\n");
i = 1;
}
printf("The elements in array are:\n");
report(list, row, column);
return 0;
}
回答by Michael
So you're a passing a pointer to int in report function:
所以你是在报告函数中传递一个指向 int 的指针:
int list[3][5];
report(&list[0][0], row, column);
...
void report(int *list, int row_count, int column_count)
...
But then you are using list in report function as pointer to pointer to int:
但是随后您在报告函数中使用列表作为指向 int 的指针的指针:
list[0][0]
But it is not a pointer to pointer to int. It just has a type of int*. So "list" is dereferenced twicely in report functions. That's wrong.
但它不是指向 int 指针的指针。它只有一种类型的 int*。所以“列表”在报告函数中被取消引用两次。那是错误的。
You can fix it if you use just "list" as parameter in function calls in report() without "[0][0]":
如果在 report() 中的函数调用中仅使用“list”作为参数而不使用“[0][0]”,则可以修复它:
rowaverage(list, row, column);
回答by chinchila
Maybe you can change the parameter list[0][0]to list. As list is a pointer to int, but list[0][0] is just an int. Their type is different, and the first parameter in your functions, a pointer is needed. :)
也许您可以将参数list[0][0]更改为list。因为 list 是一个指向 int 的指针,而 list[0][0] 只是一个 int。它们的类型不同,并且函数中的第一个参数需要一个指针。:)
回答by n. 'pronouns' m.
You are doing your arrays totally wrong everywhere.
你在任何地方都在做你的阵列完全错误。
Forget &and forget *for the time being. You don't need any in this program.
暂时忘记&和忘记*。在这个程序中你不需要任何东西。
Your listis an int[3][5], not an int*and not anything else. Declare it as such in all functions.
你list是一个int[3][5],不是一个int*,不是别的。在所有函数中声明它。
void rowaverage(int list[3][5], ...
and so on. Access it in the function like this:
等等。在函数中访问它,如下所示:
list[row][column]
not list+row*column_count+column
不是 list+row*column_count+column
Pass the list to functions like this:
将列表传递给如下函数:
rowaverage(list, ...
not rowaverage(&list[0][0], ....,
不rowaverage(&list[0][0], ....,
I have no idea why you wrote reportdifferently from all the other functions, but it doesn't matter because all the other functions are totally wrong too. If they are wirking, it's by sheer luck.
我不知道为什么你写的report与所有其他函数不同,但这并不重要,因为所有其他函数也完全错误。如果他们在绞尽脑汁,那完全是运气。
Pick up a good C book and read it.
拿起一本好的 C 书并阅读它。

