user7253227
user7253227

Reputation:

error: request for member in something not a structure or union in this code

I got the error: request for member in something not a structure or union in this code

int main() {
  char Oberon;

  struct Oberon {
    short borsa_oro;
    short punti_ferita;
    short incantesimi;
    short pozione_guaritrice; 
    short veleno;
    char armatura;
    char arma;
  };

  Oberon.borsa_oro=10;
  Oberon.punti_ferita=5;
  Oberon.incantesimi=2;
  Oberon.pozione_guaritrice=5;
  strcpy (Oberon.armatura, "Vesti del mago");
  strcpy (Oberon.arma, "Spada di Oberon");

  return 0;
}

what should I do?

Upvotes: 1

Views: 2626

Answers (2)

yLaguardia
yLaguardia

Reputation: 595

I think you meant to create something like a player struct and then create Oberon, who is a struct player.

You also forgot to associate a block of memory to armatura and arma.

You also tried to create an instance of player before the struct was defined.

So I think what you meant was this:

#include <stdio.h>
#include <string.h>

int main(void)
{

    struct player {
        short borsa_oro;
        short punti_ferita;
        short incantesimi;
        short pozione_guaritrice; 
        short veleno;
        char armatura[50];
        char arma[50];
    };

    struct player Oberon;

    Oberon.borsa_oro = 10;
    Oberon.punti_ferita = 5;
    Oberon.incantesimi = 2;
    Oberon.pozione_guaritrice = 5;
    strcpy (Oberon.armatura, "Vesti del mago");
    strcpy (Oberon.arma, "Spada di Oberon");

    printf("Oberon ha %d oro e utilizza la armatura %s\n", Oberon.borsa_oro, Oberon.armatura);

    return 0;
}

Upvotes: 3

Aravind Voggu
Aravind Voggu

Reputation: 1531

That's not how structs should be used. You also have a char with same name char Oberon; remove or rename it.

Structs just describe a data-type, you still have to initialise them, just like any other; example:

struct person {
    int age;
    ...
};

struct person p;
p.age = 10;

To simplify that, use typedef

typedef struct person {
    int age;
    ...
} person;
person p;
p.age = 10;

Upvotes: 0

Related Questions