passing by struct reference does not work (dynamic queue C)

-1

I'm trying to implement a Dynamic Queue. When I initialize the program (start and end = NULL), and then put to queue a value (function queue) the "end" pointer receives the new queue item, however the "start" does not, whereas in the function I am asking for the "start" and "end" to receive the new queue item.

Follow the code for testing:

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

struct no {
    int dado;
    struct no *prox;
};

void enfileirar(struct no **inicio, struct no *fim, int valor) {
    struct no *novoNo;

    novoNo = (struct no *) malloc(sizeof(struct no));

    novoNo->dado = valor;
    novoNo->prox = NULL;

    if (*inicio == NULL) {
        *inicio = novoNo;
    } else {
        *fim->prox = *novoNo;
    }

    *fim = *novoNo; 
}

void mostrarFila(struct no **inicio) {
    struct no *aux;
    aux = inicio;

    printf("Inicio da fila -> \n");
    while(aux == NULL) {
        printf("%d\n", &aux->dado);
        printf("\n");
        aux = aux->prox;
    }
    printf("<- Fim da fila \n");
}

int main(int argc, char** argv) {
    struct no *inicio, *fim;
    int resposta, valor;
    bool sair = true;

    while(sair) {
        printf("\n");
        printf("************** MENU **************\n");
        printf("0 - Sair do programa\n");
        printf("1 - Iniciar Fila\n");
        printf("2 - Enfileirar\n");
        printf("3 - Mostrar fila\n");
        printf("**********************************\n");

        scanf("%d", &resposta);

        switch(resposta) {
            case 0:
                sair = false;
                printf("Saindo do programa...\n");
                break;
            case 1:
                inicio = NULL;
                fim = NULL;
                break;
            case 2:
                printf("digite o valor para inserir na fila:\n");
                scanf("%d", &valor);
                enfileirar(&inicio, &fim, valor);
                printf("%d\n", &inicio->dado);
                printf("%d\n", &fim->dado);
                break;
            case 3:
                mostrarFila(&inicio);
                break;
            default:
                sair = false;
                printf("Saindo do programa...\n");
                break;
        }
    }

    return 0;
}
    
asked by anonymous 13.11.2018 / 04:01

1 answer

0

There are several problems in the code.

  • In the enfileirar() function the fim parameter must be a pointer pointer:

    void enfileirar(struct no **inicio, struct no **fim, int valor) {

  • And this forces you to put the fim pointer in parentheses in the assignment:

    (*fim)->prox = novoNo;

  • In function mostrarFila() there is no need to pass pointer to pointer since there will be no change in list

    void mostrarFila(struct no *inicio) {

  • And assignment to aux is simple:

    aux = inicio;

  • The test was wrong:

    while(aux != NULL) {

  • And you want to print the data, not its address:

    printf("%d\n", aux->dado);

  • Same thing in function main()

    printf("%d\n", inicio->dado); printf("%d\n", fim->dado);

  • And call to mostrarFila() changes to:

    mostrarFila(inicio);

  • Several of these problems were detected as warnings in the build.

        
    13.11.2018 / 11:06