Ananda
Ananda

Reputation: 1572

How to access structure variable defined in another structure?

I have following structure

typedef struct List_Node {
     struct File_Descriptor *data;
     char *key;
    struct List_Node *next;
}List_Node;

Now I inserted some values into the both the structures and want to access the data of type File_descriptor. How to do this?

I tried this

struct List_Node *ln1;
printf("%s", ln1.File_Descriptor->data);

but it is giving error

error: request for member ‘error: File_Descriptor’ in something not a structure or union` 

Upvotes: 0

Views: 572

Answers (2)

nickolayratchev
nickolayratchev

Reputation: 1206

I believe you are confusing the type name with the variable name. In order to access the data member of the List_Node struct, you use the following:

struct List_Node *ln1; // initialize this
printf("%s", ln1->data);

Don't forget that you first have to initialize the ln1 pointer to point to a valid memory location before dereferencing it.

Upvotes: 1

mattjgalloway
mattjgalloway

Reputation: 34902

You just want:

struct List_Node *ln1;
printf("%s", ln1->data);

struct File_Descriptor is the type. data is the struct member name.

Also though the printf format looks entirely wrong. Not sure what you're trying to do there. %s is string, and data certainly doesn't look like a string.

Upvotes: 2

Related Questions