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
#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;
}
Looking over I have seen that memory is not initialized for the variable, it may have other errors. The code is confused and disorganized.
– Maniero