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.
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?
– paraguassu
That’s a constant like
Placa
as1964
is for theanofab
that isint
. So you don’t need a variablePlaca
inmain()
. Just one example. And anyway the value will be reset viasprintf()
afterward.– arfneto