简体   繁体   中英

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 cartas which 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;
}```

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM