Problem assigning values to a struct in C

Asked

Viewed 65 times

0

Hello, I know how to program in other languages but I’m a beginner in C and I’m burning my brains out doing a job for the college that asks to store information of 10 vehicles in an array of structs. In this struct, the boards are defined as XXX-YYYY (where x are the letters and y the numbers) and to have this organization I created a separate struct for the board and inside the vehicle I use a die of type PLATE, however, when calling the value of the board, my return to board.letters is the complete given instead of coming only the letters, come all and on board.numeros comes only the numbers. Follows the code.

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

// Struct Placa
typedef struct placa
{
  char letras[3];
  char numeros[4];
} PLACA;

// Struct Veiculo
typedef struct veiculo
{
  char marca[50];
  char modelo[50];
  int anofab;
  struct placa placa;
} VEICULO;

To follow my duties:

void listarveiculos(VEICULO veiculos[])
{
  for (int i = 0; i < 10; i++)
  {
    if (strlen((veiculos[i].modelo)) != 0)
    {
      printf("Marca: %s\n", veiculos[i].marca);
      printf("Modelo: %s\n", veiculos[i].modelo);
      printf("Ano de Fabricação: %s\n", veiculos[i].anofab);
      exibirplaca(veiculos[i].placa);
    }
  }
}

void exibirplaca(PLACA placa)
{
  char placaformatada[8] = placa.letras;
  strcat(placaformatada, "-");
  strcat(placaformatada, placa.numeros);

  strupr(placaformatada);

  printf("Placa: %s\n\n", placaformatada);
}

when trying to compile get the following error:

veiculos.c: In function 'listarveiculos':
veiculos.c:55:7: warning: implicit declaration of function 'exibirplaca' [-Wimplicit-function-declaration]
       exibirplaca(veiculos[i].placa);
       ^~~~~~~~~~~
veiculos.c: At top level:
veiculos.c:60:6: warning: conflicting types for 'exibirplaca'
 void exibirplaca(PLACA placa)
      ^~~~~~~~~~~
veiculos.c:55:7: note: previous implicit declaration of 'exibirplaca' was here
       exibirplaca(veiculos[i].placa);
       ^~~~~~~~~~~
veiculos.c: In function 'exibirplaca':
veiculos.c:62:28: error: invalid initializer
   char placaformatada[8] = placa.letras;
                            ^~~~~

I’ve burned my cuckoo here.

1 answer

2


i use a die of type PLATE, however, when calling the value of the plate, my return to plate.letters is the complete given instead of coming only letters, come all and on board.numeros come only numbers

In C strings are null-terminated, has a zero at the end. For power %s in the printf() and string functions. h would need to have a 0 between letters and numbers on each board, and another zero at the end.

Its structures have a less than practical level: it is more convenient to have Veiculos as a collection of Veiculo, each Veiculo with your Placa.

Take an example:

It’s just a skeleton, but it should help to understand the notation in that language and the pitch of another level of encapsulation to the data, creating Veiculos.

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    char    letras[4];
    char    numeros[5];

}   Placa;

typedef struct
{
    char    marca[50];
    char    modelo[50];
    int     anofab;
    Placa   placa;

}   Veiculo;

typedef struct {
    int     N;
    Veiculo v[10]; // capacidade fixa: 10

}   Veiculos;

void listar_veiculos(Veiculos*);

int         main(void)
{
    srand(210820);
    Veiculo     um = { "Aston Martin", "DB5", 1964, (Placa){ "ABC", "1234" } };
    Veiculos    frota;
    frota.N = 0; // sem veiculos

    // cria uns
    while ( frota.N < 8 )
    {
        frota.v[frota.N] = um;
        // inventa novo numero para a placa
        sprintf( frota.v[frota.N].placa.numeros, "%04d", rand() % 10000 );
        frota.N += 1;
    };
    listar_veiculos(&frota);
    return 0;
}

void listar_veiculos(Veiculos* f)
{
    printf("%d veiculos no total\n", f->N);
    for (int i = 0; i < f->N; i++)
    {
        printf("    %s %s Ano %d Placa: %s-%s\n",
            f->v[i].marca,
            f->v[i].modelo,
            f->v[i].anofab,
            f->v[i].placa.letras,
            f->v[i].placa.numeros
            );
    };  // for()
};

Which shows

8 veiculos no total
    Aston Martin DB5 Ano 1964 Placa: ABC-1539
    Aston Martin DB5 Ano 1964 Placa: ABC-2774
    Aston Martin DB5 Ano 1964 Placa: ABC-7665
    Aston Martin DB5 Ano 1964 Placa: ABC-1366
    Aston Martin DB5 Ano 1964 Placa: ABC-6816
    Aston Martin DB5 Ano 1964 Placa: ABC-3295
    Aston Martin DB5 Ano 1964 Placa: ABC-9245
    Aston Martin DB5 Ano 1964 Placa: ABC-9244

Having a container for the set of cars is simpler to write and control.

  • Thank you very much for the explanation and the suggestions. In fact, using a container for the vehicles made handling much easier. There’s only one thing I didn’t get right: (Board){ "ABC", "1234" } in this part, this is a way to make explicit the type of value?

  • That’s a constant like Placa as 1964 is for the anofab that is int. So you don’t need a variable Placa in main(). Just one example. And anyway the value will be reset via sprintf() afterward.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.