martiwg
martiwg

Reputation: 95

how do I assign a value to a struct array inside a struct in c?

So I need to assign the scanned values to the variables palo and valor which belong to cartaswhich is a struct, and this one is inside another struct?

This is what I've got so far and i get an error "must have pointer-to-object type":

#include <stdio.h>
#define DIM 100

typedef struct{
    char palo;
    int  valor;
}t_carta;

typedef struct{
    int ncartas;
    t_carta cartas[DIM];
}t_baraja;

int main(){
    t_baraja b1;
    t_carta carta[DIM][DIM];

    printf("Cuantas cartas tiene su baraja? ");
    scanf("%d", &b1.ncartas);
    printf("Introduzca las cartas separadas por guiones (o4-e10-b1...):\n");
    for(int i=0; i<b1.ncartas; i++){
        scanf("%c%d%*c", &b1.cartas->palo, &b1.cartas->valor);
        b1.cartas[i][i]=b1.cartas->valor;
    }

    return 0;
}```

Upvotes: 1

Views: 62

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

The presented code does not make a sense nevertheless answering your question

how do I assign a value to a struct array inside a struct in c?

I will explain how you can do this.

If you have an object declared like

t_baraja b1;

then to set data members of its data member array

 t_carta cartas[DIM];

you can the following way

b1.cartas[i].palo; = some_value;
b1.cartas[i]valor = another_value;.

where i is an index that selects an element in the data member array cartas.

Upvotes: 1

Related Questions