Reputation: 25
i'm learnig how to code in C. i have to create a record for a person, with an birthday and a ID number the code is composed of 3 files
the fist is a header
// definição do tipo
typedef int vetor[10];
typedef struct
{
char nome[50];
vetor nasceu;
vetor cpf;
} dados ;
void cadastro (dados*, char[50], vetor, vetor);
then there is the definitions of the header
#include <stdio.h>
#include <string.h>
#include "cadastro.h"
void cadastro (dados *usuario, char name[50], vetor ddn, vetor cpf)
{
int i;
strcpy(usuario->nome,name);
for (i = 0; i < 50; i++)
{
usuario->nasceu[i] = ddn[i];
}
for (i = 0; i < 10; i++)
{
usuario->cpf[i] = cpf[i];
}
}
and the last file uses the header to generate the record
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cadastro.h"
int preenche_cadastro (char a[],vetor b,vetor c)
{
int i;
printf ("inserir nome: ");
gets (a);
printf("inserir data de nascimento (campos separados por espaco): ");
for (i = 0; i < 3; i++)
{
scanf("%d",&b[i]);
}
printf ("inserir CPF (campos separados por espaco): ");
for (i = 0; i < 4; i++)
{
scanf("%d",&c[i]);
}
return (0);
}
int imprime_cadastro (dados usuario)
{
printf("\nnome: %s",usuario.nome);
printf("\ndata de nascimento: %d / %d / %d\n", usuario.nasceu[0],usuario.nasceu[1],usuario.nasceu[2]);
printf("CPF: %d . %d . %d - %d\n", usuario.cpf[0],usuario.cpf[1],usuario.cpf[2],usuario.cpf[3]);
return(0);
}
int main(void)
{
dados new_entry;
char name[50];
vetor born, cpf;
int i;
preenche_cadastro (name,born,cpf);
cadastro(&new_entry, name, born, cpf);
imprime_cadastro(new_entry);
return (0);
}
i don't really know how to debug, but as far as i could tell, the `Segmentation fault' occurs only at the line
return (0);
i'm going mad here, can anybody help me?
sorry for my english, it's not mother language
Upvotes: 1
Views: 633
Reputation: 75062
You invoked undefined behavior by accessing out-of-range of array in the line
usuario->nasceu[i] = ddn[i];
in function cadastro
, and then the program happened to crash there.
Do not invoke undefined behavior. Instead of using magic number 10
, you should define the number of elements in the array and use it.
Also note that using values of uninitialized variables having automatic storage duration, which are indeterminate, also invokes undefined behavior.
corrected header:
// definição do tipo
#define VETOR_NUM 10
typedef int vetor[VETOR_NUM];
typedef struct
{
char nome[50];
vetor nasceu;
vetor cpf;
} dados ;
void cadastro (dados*, char[50], vetor, vetor);
corrected implementation of cadastro
:
#include <stdio.h>
#include <string.h>
#include "cadastro.h"
void cadastro (dados *usuario, char name[50], vetor ddn, vetor cpf)
{
int i;
strcpy(usuario->nome,name);
for (i = 0; i < VETOR_NUM; i++)
{
usuario->nasceu[i] = ddn[i];
}
for (i = 0; i < VETOR_NUM; i++)
{
usuario->cpf[i] = cpf[i];
}
}
corrected main()
function:
int main(void)
{
dados new_entry;
char name[50];
vetor born = {0}, cpf = {0}; /* initialize arrays */
/* i is removed because it wasn't used */
preenche_cadastro (name,born,cpf);
cadastro(&new_entry, name, born, cpf);
imprime_cadastro(new_entry);
return (0);
}
One more note: You shouldn't use gets()
, which has unavoidable risk of buffer overrun.
Upvotes: 3
Reputation: 1991
This loop in your cadastro
function:
for (i = 0; i < 50; i++)
{
usuario->nasceu[i] = ddn[i];
}
Seems to not be in line with the size of the nasceu
arrary:
typedef int vetor[10];
typedef struct
{
char nome[50];
vetor nasceu;
vetor cpf;
} dados ;
Have you tried changing to:
for (i = 0; i < 10; i++)
{
usuario->nasceu[i] = ddn[i];
}
?
Upvotes: 2
Reputation: 365
Your first loop should only copy 10 items rather than 50.
for (i = 0; i < 10; i++)
{
usuario->nasceu[i] = ddn[i];
}
Upvotes: 3