Thread: Submenus and passing values by reference

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    61

    Submenus and passing values by reference

    Hi people

    Need help to pass variables with submenus, it was working before i put the menus.

    I need to change total on a submenu but i don´t know how.

    I want to pass the reference not a “copy”

    1º I receive a copy on
    int menu(pArea vector, int total, pspecies lista_animais);

    2º i pass it
    submenu1(vector, &total, lista_animais);

    3º Then i receive it
    int submenu1(pArea vector, int * total, pspecies lista_animais)


    Then here is ok imprime_areas(vector, *total);


    My problem is here how i pass by reference ?
    vector = recebe_area_utilizador(vector, &total);

    I receive the following warnings
    main.c:132:57: warning: passing argument 2 of 'recebe_area_utilizador' makes integer from pointer without a cast [-Wint-conversion]
    case 4: vector = recebe_area_utilizador(vector, &total);
    ^
    In file included from main.c:3:0:
    VectorDinamico.h:42:7: note: expected 'int' but argument is of type 'int **'
    pArea recebe_area_utilizador(pArea vector, int total);

    Code:
    int menu(pArea vector, int total, pspecies lista_animais) {
        int tipo1;
    
    
        printf("number after insert %d\n", total);
    
    
    
    
        printf("\n1- Areas \n2- Segundo menu\n3- Terceiro menu\n4- Quarto menu\n\n");
        scanf("%d", &tipo1);
        getchar();
    
    
        switch (tipo1) {
            case 1: printf("Entrou no primeiro menu!\n");
                submenu1(vector, &total, lista_animais);
                break;
            case 2: printf("Entrou no segundo menu!\n");
                //  submenu2(vector, total, lista_animais);
                break;
            case 3: printf("Entrou no terceiro menu!\n");
                // submenu3(vector, total, lista_animais);
                break;
            case 4: printf("Entrou no quarto menu!\n");
                // submenu4(vector, total, lista_animais);
                break;
            default: printf("Introduza o valor correcto respectivo ao menu que quer selecionar!\n");
                menu(vector, total, lista_animais);
        }
    }
    
    
    int submenu1(pArea vector, int * total, pspecies lista_animais) {
        int tipo2;
        printf("\n1- Imprime todas as areas\n2- Segundo sub-menu1\n3- Terceiro sub-menu1\n4- Nova Area\n\n");
        scanf("%d", &tipo2);
        getchar();
    
    
        switch (tipo2) {
            case 1:
                imprime_areas(vector, *total);
                break;
            case 2: printf("Entrou no segundo sub-menu1!\n");
                break;
            case 3: printf("Entrou no terceiro sub-menu1!\n");
                break;
            case 4: vector = recebe_area_utilizador(vector, &total);    // HOW ???        
                break;
            default: printf("Introduza o valor correcto respectivo ao menu que quer selecionar!\n");
                submenu1(vector, total, lista_animais);
        }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You cannot pass the total by reference since recebe_area_utilizador is declared as having a second parameter (corresponding to the total) of type int, not int*. If the second parameter was of type int*, then it would be the same as with submenu1:
    Code:
    vector = recebe_area_utilizador(vector, total);
    where total is the pointer parameter of submenu1, pointing to the underlying total object in menu.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-20-2015, 09:22 PM
  2. passing int values and *double values through message queues
    By Hyp3rTension in forum C Programming
    Replies: 11
    Last Post: 05-11-2012, 05:04 AM
  3. Returning values by reference.
    By Kitt3n in forum C++ Programming
    Replies: 2
    Last Post: 05-28-2010, 09:16 AM
  4. Replies: 6
    Last Post: 04-04-2010, 11:48 AM
  5. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 01:25 AM

Tags for this Thread