无法为结构分配内存,错误: request for member '****' in something not a structure or union

标签 c struct dynamic-memory-allocation

我最近正在做学校作业,但在创建动态结构数组时遇到了困难。我到处寻找,但没有答案帮助我,所以你能看看我的代码并帮助我,出了什么问题吗? 我不断收到错误: C:\kof\test1\main.c|29|错误:请求非结构或 union 中的成员“长度”|

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct time
{
int mi;
int ho;
int dn;
int me;
int ro;
};

struct journey
{
int lenght;
struct time zac;
struct time kon;
int tank;
int price;
};


int main ()
{
struct journey *fail;
struct journey *b=(struct journey*) malloc (1*sizeof(struct journey));
fail=b;
fail.lenght=5;
return 0;
}

最佳答案

由于 *fail 是 struct Journey 类型的指针,因此您可以使用“->”表示法而不是使用 来访问其元素。 符号如所述 here

关于无法为结构分配内存,错误: request for member '****' in something not a structure or union,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20719784/

相关文章:

c - 启动 SRAM 读操作

c - 递归构建字符串

c - 整数大小的变化?

c - 哪些IO口用于编码引擎、传感器

c - c中链表的升序

c - 在一个 block 中为不同类型分配内存

c - malloc() : memory corruption even if i check result of memory allocation function

Swift - 如何在迭代结构对象时改变它

c - 释放结构数组

c++ - 为什么我的删除功能总是出现段错误