Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing structure variable through pointer

#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedef struct male_node
{
    int mv1,mv2,mv3;
}male;

typedef struct movie_name
{
    char mvnm[20];
    struct male *ml;
}movie;

main()
{
    movie mov1;
    mov1.ml=(male*)malloc(sizeof(male));
    mov1.(*ml).mv1=1;//ERROR
    mov1.ml->mv1=1; //ERROR
}

How to access the mv1 variable through mov1 and ml I tried to access mv1 through ml which is a pointer to a structure which is again a structure variable but it shows error.

like image 277
user971089 Avatar asked Mar 22 '26 05:03

user971089


1 Answers

This looks wrong:

struct male *ml;

You've used typedef to define male as struct male_node, so it doesn't make sense to say struct male. Instead, try this:

typedef struct movie_name
{
    char mvnm[20];
    male *ml;
} movie;

That should fix your problem, and you should be able to do this:

mov1.ml->mv1=1;
like image 128
Caleb Avatar answered Mar 24 '26 18:03

Caleb