Struct vector: Passage by reference in C

Asked

Viewed 321 times

1

This code is from a simple register of people, containing code and name of the person. I am confused about the passage from the struct vector to the "insert procedure".

When compiling, the program stops working. In the compiler I have the following error: "Program Received Signal sigsegv Segmentation fault". I saw that this error has something to do with incorrect reference to a pointer, but I don’t know how to fix it.

Follows the code:

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <string.h>
struct pessoas
{
	int codigo;
	char nome [50];
};

	struct pessoas vetpessoas[5];
	int k = 0;


void inserir (struct pessoas *vetpessoas, int *k)
{
	char nomeaux [50];
	int codaux, i, j;
	char resp, resp1;
	
	resp = 's';
	
	if (*k > 0)
	{
		i = *k;
	}
	else 
	{
		i = 0;
	}
	
	do
	{
		do
		{
			printf ("Entre com o codigo:");
			scanf("%i",codaux);
		}
		while (codaux < 0);
		
		do
		{
			printf ("Entre com o nome:");
			scanf ("%s",nomeaux);
		}
		while (nomeaux == "");
		
		do
		{
			j = j+1;
		}
		while (codaux != vetpessoas[j].codigo && j<i);
		
		if (j==i)
		{
			do
			{
				printf("Confirma a inclusao (S/N) ?");
				scanf ("%s",resp1);
			}
			while (resp1!='s' || resp1!='n');
			
			if (resp1 =='s')
			{
				vetpessoas[i].codigo = codaux;
				strcpy(vetpessoas[i].nome, nomeaux);
				printf ("Inclusao efetuada com sucesso!");
			}
			else 
			{
				printf ("Codigo ja cadastrado!");
				i = i-1;
			}
		}
		
		if (i < 4)
		{
			do
			{
				printf("Deseja continuar incluindo (S/N) ?");
				scanf("%c",resp);
			}
			while (resp1!='s' || resp1!='n');
		}
		i = i+1;
	}
	while (resp=='s' && i<=4);
	
	*k = i;
	
	if (i > 4)
	{
		printf ("Vetor já está Cheio! Não é permitido mais armazenar valores!!!");
	}

}

main ()
{	

    inserir (vetpessoas, &k);
	return 0;

}

  • One of the problems I see is that the code is too confusing, it seems to have a lot of unnecessary things, which makes it easier to make mistakes. Do you have any reason to do all this or is it a mistake too?

  • If you refer to "do while" is that the program needs to check each data entry, this ended up getting extensive.

  • No, I mean a lot of things, and that check is not enough, I still don’t get other possible mistakes. And I started to rewrite but a flaw in the Internet made me lose and I didn’t want to do it again, because to fix it would change so much that it would misread your code, the way you are I think you will learn a lot of things the wrong way, not worth touching it, I’m sorry. Just to get an idea my code is already almost 1/3 the size of yours.

  • Yes, there is still a lot that is not being validated. In fact the code is adapted from an algorithm that the teacher passed. I imagine your version is much more simplified. Thank you for your willingness to help.

1 answer

1


The problem of your code is in the readings: in scanf you must pass the memory address of the variable that will receive the reading, and not the variable itself. Below is your code with correction.

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <string.h>
struct pessoas
{
    int codigo;
    char nome [50];
};

struct pessoas vetpessoas[5];
int k = 0;


void inserir (struct pessoas *vetpessoas, int *k)
{
    char nomeaux [50];
    int codaux, i, j;
    char resp, resp1;

    resp = 's';

    if (*k > 0)
    {
        i = *k;
    }
    else 
    {
        i = 0;
    }

    do
    {
        do
        {
            printf ("Entre com o codigo:");
            scanf("%i", &codaux);
        }
        while (codaux < 0);

        do
        {
            printf ("Entre com o nome:");
            scanf ("%s", &nomeaux);
        }
        while (nomeaux == "");

        do
        {
            j = j+1;
        }
        while (codaux != vetpessoas[j].codigo && j<i);

        if (j==i)
        {
            do
            {
                printf("Confirma a inclusao (S/N) ?");
                fflush(stdin);
                scanf ("%c", &resp1);

                if (resp1 == 's' || resp1 == 'n')
                    break;
            }
            while (1);

            if (resp1 =='s')
            {
                vetpessoas[i].codigo = codaux;
                strcpy(vetpessoas[i].nome, nomeaux);
                printf ("Inclusao efetuada com sucesso!");
            }
            else 
            {
                printf ("Codigo ja cadastrado!");
                i = i-1;
            }
        }

        if (i < 4)
        {
            do
            {
                fflush(stdin);
                printf("Deseja continuar incluindo (S/N) ?");
                scanf("%c", &resp);

                if (resp == 's' || resp == 'n')
                    break;
            }
            while (1);
        }
        i = i+1;
    }
    while (resp=='s' && i<=4);

    *k = i;

    if (i > 4)
    {
        printf ("Vetor já está Cheio! Não é permitido mais armazenar valores!!!");
    }

}

main ()
{   

    inserir (vetpessoas, &k);
    return 0;

}

Browser other questions tagged

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