此代码有什么问题,我看不到编译错误。我想创建一个结构类型指针结构数组。然后我想分配x和y坐标,最后我要打印所有坐标。我在地方做了(* pp).x,在其他地方做了pp-> x。因为两个都是同一个错误。请告诉我。
#include <stdio.h>
#include <conio.h>
#define MAX_STRUCT 10
struct point{
int x;
int y;
};
void PrintIt(int x, int y);
main()
{
struct point *pp[MAX_STRUCT];
printf("Enter how many coordinates you want to input: ");
int count=0, n;
scanf("%d", &n);
while(count<n){
printf("\nEnter the X & Y coordinate of the point: ");
int i,j;
scanf("%d%d", &i, &j);
(*pp[count]).x=i; /* or we can do this : pp[count] -> x same thing
(*pp[count]).y=j; // or we can do this : pp[count] -> y same thing
count++;
}
PrintIt(pp[count]->x, pp[count]->y);
return 0;
}
void PrintIt(int x, int y)
{
printf("\n(%d,%d)", x, y);
}
答案 0 :(得分:1)
您需要为pp
动态分配内存:
#include <stdlib.h>
struct point *pp = malloc(MAX_STRUCT * sizeof(*pp)); // sizeof(struct point) would work too
或者,不要指出它们:
struct point pp[MAX_STRUCT];