1

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;
}```
3
  • 1
    @martiwg The data member t_carta cartas[DIM]; is a one-dimensional array. So why are you using two indices b1.cartas[i][i]=b1.cartas->valor;? This statement does not make a sense. Commented Nov 11, 2020 at 10:30
  • @martiwg And what is this array t_carta carta[DIM][DIM]; doing? Commented Nov 11, 2020 at 10:32
  • @VladfromMoscow I don't know my teacher sent this as a header and didn't really understand how i could assign the values to the variables, but your response fixed it all! tysm Commented Nov 11, 2020 at 14:29

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks! I know it didn't make sense so that's why i was asking anyway

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.