user2952320
user2952320

Reputation: 65

C - New with structs

I am trying to figure out how to use struct but this code gives me a lot of errors.

#include <stdio.h>

int main(void)
{
    struct date
    {
        int today       =   6;
        int tomorrow    =   7;
        int threeDays   =   8;
    };

    struct date date;

    printf("%d", date.today);

    return 0;
}

Upvotes: 1

Views: 75

Answers (2)

plinth
plinth

Reputation: 49179

You can't define a struct within a function.

#include <stdio.h>

struct date { int today, tomorrow, threeDays; };

int main(void)
{
    struct date adate = { 6, 7, 8 };

    printf("%d", adate.today);
}

Upvotes: -4

ouah
ouah

Reputation: 145829

struct date
{
    int today       =   6;
    int tomorrow    =   7;
    int threeDays   =   8;
};

struct date date;

You cannot assign a default value to a structure type.

What you can do is to initialize an object of a structure type with the correct values:

struct date
{
    int today;
    int tomorrow;
    int threeDays;
};

struct date date = {6, 7, 8};

Upvotes: 6

Related Questions