Doubts in code C data structure

Asked

Viewed 117 times

0

Good afternoon, good I’m here to see if anyone knows why my code is printing a junk memory, when run it shows what I want but it prints a junk memory, if anyone knows why thank you

esses numeros ai "7304688"

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

typedef struct no

int codigo;
char nome[10];
struct no *next;
}no;

struct no *corrente, *auxiliar, *inicio;

int i;

int inserir (no *lista)
{
    for(i=1;i<=3;i++)
    {
        if(auxiliar==NULL){
            auxiliar = (no*)malloc(sizeof(no));
            auxiliar->next = NULL;
            corrente = auxiliar;
            inicio = auxiliar;
        }
        else{
            auxiliar = (no*)malloc(sizeof(no));
            corrente->next=auxiliar;
            auxiliar->next = NULL;
            corrente = auxiliar;
            fflush(stdin);
            printf("Entre com o codigo:\n");
            scanf("%d",&auxiliar->codigo);
            fflush(stdin);
            printf("Entre com o nome:\n");
            scanf("%s",&auxiliar->nome);
        }
    }
}



int exibir(no *lista)
{

    if(inicio==NULL){

        printf("Lista Vazia\n");
        system("pause");
    }
    else{
        corrente = inicio;
        //system("cls");
        //printf("\tDados a ser mostrados...");
        //sleep(5);
        //system("cls");


        while(corrente!=NULL){

            printf("%d\n",corrente->codigo);
            printf("%s\n",corrente->nome);
            corrente = corrente->next;

        }
        system("pause");
    }
}

int main (){

    auxiliar = NULL;
    corrente = NULL;
    inicio = NULL;
    int tecla=1;


    printf("Programando dinamicamente:\n");
    printf("Aplicando encadeamento...\n");

    while(tecla!=0){
        printf("Digite\n \n1-Inserir \n2-Mostrar\n");
        scanf("%d",&tecla);


        switch(tecla){
            case 1: inserir(&inicio);
            break;
            case 2: exibir(&inicio);
            break;
            default: printf("Opcao Invalida");
            break;
        }
    }
    return 0;

}
  • 1

    Looking over I have seen that memory is not initialized for the variable, it may have other errors. The code is confused and disorganized.

1 answer

0


Where to start?

First of all, avoid using global variables whenever possible. In particular, define a global int i to use as an index for loop is to get into infinite confusion, since you always have to remember if some other function that is calling your current function uses that same variable to control a loop your. As here only the inserir() uses the variable i, this for now will not happen, but at the same time it is unjustifiable it be global if only a function should see it.

Secondly, when it comes time to do scanf(), always start to string format with a space for the CRT to "eat" extra characters as car returns. When reading a string, always use the precision specifier so that the buffer Do not burst: then scanf(" %.9s", &(auxiliar->nome)); on line 35, for example (note that we are having at most 9 carecters, so that the tenth contains the ' 0').

Third, you pass a parameter no * lista so much for inserir() how much to exibir(), and then solemnly ignores the parameters you passed in favor of global references *inicio, *corrente and *auxiliar. Create instead a local variable of type no * and use it instead of corrente, Initiating it with the value of inicio on the principle of function.

There are other things, but none of that makes your code print out the trash. What it does is inserir(): you noticed that she has a loop for(i=1;i<=3;i++), but when you run option 1 for the first time it just asks two names? Let’s make a Chinese insert:

  1. Starts the function. i = ???, inicio = NULL, corrente = NULL, auxiliar = NULL.
  2. Get in the loop. i = 1, inicio = NULL, corrente = NULL, auxiliar = NULL.
  3. if: as auxiliar == NULL, you enter "then".
  4. the auxiliar. i = 1, inicio = NULL, corrente = NULL, auxiliar = { .codigo = ???, .nome = ???, next = ??? }.
  5. arrow the auxiliar->next. i = 1, inicio = NULL, corrente = NULL, auxiliar = { .codigo = ???, .nome = ???, next = NULL }.
  6. arrow the corrente. i = 1, inicio = NULL, corrente = { .codigo = ???, .nome = ???, next = NULL }, auxiliar = { .codigo = ???, .nome = ???, next = NULL }.
  7. arrow the inicio. i = 1, inicio = { .codigo = ???, .nome = ???, next = NULL }, corrente = { .codigo = ???, .nome = ???, next = NULL }, auxiliar = { .codigo = ???, .nome = ???, next = NULL }.
  8. the end of the loop, increment, test. Loop continues. i = 2, inicio = { .codigo = ???, .nome = ???, next = NULL }, corrente = { .codigo = ???, .nome = ???, next = NULL }, auxiliar = { .codigo = ???, .nome = ???, next = NULL }.
  9. auxiliar != NULL, then goes to the else. i = 1, inicio = { .codigo = ???, .nome = ???, next = NULL }, corrente = { .codigo = ???, .nome = ???, next = NULL }, auxiliar = { .codigo = ???, .nome = ???, next = NULL }.
  10. A new one is allocated auxiliar, with no data. i = 1, inicio = { .codigo = ???, .nome = ???, next = NULL }, corrente = { .codigo = ???, .nome = ???, next = NULL }, auxiliar = { .codigo = ???, .nome = ???, next = ??? }.
  11. arrow the corrente->next to the new auxiliar and then you start filling in your fields. i = 2, inicio = { .codigo = ???, .nome = ???, next = &auxiliar }, corrente = { .codigo = ???, .nome = ???, next = &auxiliar }, auxiliar = { .codigo = 1234, .nome = "joao", next = NULL }.

Can you see the pattern? The first link of the list had neither codigo nor nome initialized. Then it prints the trash that was in the codigo and the garbage that was in nome (in my case, thank God, he immediately found a byte 0 and printed a string empty.

What to do then?

Redo your inserir to test the existence of auxiliarbefore to enter the loop, and then load the information into all the iterations of for. So he’ll stop inserting garbage.

Browser other questions tagged

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