Cannot allocate memory for struct, error: request for member '****' in something not a structure or union

StackOverflow https://stackoverflow.com/questions/20719784

Question

I am recently doing a school homework and I am stuck, in creating dynamic array of structs. I was looking everywhere, but no answer helped me, so can you please look at my code and help me, what is wrong? I keep getting error: C:\kof\test1\main.c|29|error: request for member 'lenght' in something not a structure or 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;
}
Was it helpful?

Solution

Since *fail is a pointer of type struct journey, you access its elements using the '->' notation and not using the . notation as stated here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top