El Fufu
El Fufu

Reputation: 66

How make a Struct in C

I am currently trying to figure out how structs work in C.

I assume that this code is setting the values x and y for the respective position1, position2 to the assigned ones.

However, I keep getting the following error message:

error: expected '=', ',', ';', 'asm' or '__attribute__; before '.' token

Here is the code:

struct position {
    int x;
    int y;
} position1, position2;

position1.x = 60;
position1.y = 0;
position2.x = 60;
position2.y = 120;

Why is it throwing me that error?

Upvotes: 1

Views: 88

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

It seems the problem is that you are trying to place these assignment statements outside a function. You may not do this. You may place only declarations outside a function.

So write the following way

struct position {
    int x;
    int y;
} position1 = { 60, 6 }, position2 = { .x = 60, .y = 120 };

Also inside a function you may use a compound literal to assign a value to an object of the structure.

Here is a demonstrative program

#include <stdio.h>

struct position {
    int x;
    int y;
} position1 = { 60, 6 }, position2 = { .x = 60, .y = 120 };

int main( void )
{
    printf( "position1: { %d, %d }\n", position1.x, position1.y );
    printf( "position2: { %d, %d }\n", position2.x, position2.y );

    position1 = ( struct position ) { 10, 20 };
    position2 = ( struct position ) { .x = 30, .y = 40 };

    printf( "position1: { %d, %d }\n", position1.x, position1.y );
    printf( "position2: { %d, %d }\n", position2.x, position2.y );

    position1.x = 50;
    position1.y = 60;

    position2.x = 70;
    position2.y = 80;    

    printf( "position1: { %d, %d }\n", position1.x, position1.y );
    printf( "position2: { %d, %d }\n", position2.x, position2.y );

    return 0;
}

Its output is

position1: { 60, 6 }
position2: { 60, 120 }
position1: { 10, 20 }
position2: { 30, 40 }
position1: { 50, 60 }
position2: { 70, 80 }

Upvotes: 2

Alex Reynolds
Alex Reynolds

Reputation: 96974

Try:

#include <stdlib.h>

struct position_t {
    int x;
    int y;
};

int main()
{
    struct position_t position1, position2;

    position1.x = 60;
    position1.y = 0;
    position2.x = 60;
    position2.y = 120;

    return 0;
}

Another way is to use typedef to give the struct an alias:

#include <stdlib.h>

typedef struct position_t {
    int x;
    int y;
} position;

int main()
{
    position position1, position2;

    position1.x = 60;
    position1.y = 0;
    position2.x = 60;
    position2.y = 120;

    return 0;
}

Upvotes: 2

Kninnug
Kninnug

Reputation: 8053

You can initialize your global structs like this:

struct position {
    int x;
    int y;
} position1 = {60, 0}, position2 = {60, 120};

Or, with a little bit more clarity and designated initializers:

    ...
} position1 = {.x = 60, .y = 0}, 
  position2 = {.x = 60, .y = 120};

Upvotes: 3

zuko32
zuko32

Reputation: 239

You just have to put all the code after the struct declaration inside the main() function and it does not throw any error. Like that:

#include <stdio.h>

struct position {
    int x;
    int y;
} position1, position2;

int main(void) {
    position1.x = 60;
    position1.y = 0;
    position2.x = 60;
    position2.y = 120;

    printf("%d\n", position2.y); // this line is just for testing
}

Upvotes: 0

Related Questions