我很困惑如何使用指针表示法访问malloc使用的结构。我可以使用数组表示法,但指针上的语法是什么?如何使用指针表示法访问动态结构
#include "stdio.h"
#include "stdlib.h"
using namespace std;
typedef struct flightType {
int altitude;
int longitude;
int latitude;
int heading;
double airSpeed;
} Flight;
int main() {
int airbornePlanes;
Flight *planes;
printf("How many planes are int he air?");
scanf("%d", &airbornePlanes);
planes = (Flight *) malloc (airbornePlanes * sizeof(Flight));
planes[0].altitude = 7; // plane 0 works
// how do i accessw ith pointer notation
//*(planes + 1)->altitude = 8; // not working
*(planes + 5) -> altitude = 9;
free(planes);
}
我在接收错误 - main.cpp | 34 |错误:请求'(飞机+ 120u)'中的成员'altitude',它是非班级类型'Flight *'| || ===构建完成:1个错误,0个警告=== | – runners3431
如果这是一个c代码,请使用stdio而不是iostream和名称空间。那些是C++的。 – halfelf