johan
johan

Reputation: 1983

how to define a global "struct" variable in C

I would like to define a global/public variable that is created by struct. I can not access user_list.x or y/z inside the main or any another function. when I debug the code below, I get the following error "request for member 'x' in something not a structure or union". How can I declare a global struct variable that I can access from different functions? Thank you

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

struct routing {
        int x;
        int y;
        int z;
};
struct routing user_list[40];

int main(int argc,char *argv[])
{

    user_list.x = 1;
    printf("user_list.x is %d",user_list.x);
    return 0;
}

Upvotes: 1

Views: 11351

Answers (2)

DonCallisto
DonCallisto

Reputation: 29912

You have to access by index to your array.

Try

user_list[i].x = 1;
printf("user_list[i].x is %d",user_list[i].x);

Where i is your index (previously defined, or "constant")

Upvotes: 1

ouah
ouah

Reputation: 145829

You are using an array of struct objects not a struct object.

Specify the array member you want to access:

int i = 0;

user_list[i].x = 1;
printf("user_list[i].x is %d",user_list[i].x);

Upvotes: 9

Related Questions