Thread: Pointer doubt

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    159

    Pointer doubt

    Hi

    I'm finishing my game but now i just have a little problem concerning pointers..

    My first function is

    1º function

    Code:
    int gp1_Ques(struct perg *p, char *usr1) //arguments needed)
    gp2_Ques(&p, usr1, &result, &total_perg, &certas, &erradas); //passing all this arguments to next function
    2º function
    To this function a use pointers

    Code:
    int gp2_Ques(struct gp2 *p, char *usr1, int *result,int *total_perg,int *certas,int *erradas) //arguments needed from function1
    gp3_Ques(usr1, *result, *total_perg, *certas, *erradas); //passing all values to next function
    printf("\nTem um total de %d pontos\n", *result); //get the result
    3º function
    I can't get the right values, i use for example
    printf("\nTem um total de %d pontos\n", *result);

    Code:
    int gp3_Ques(char *usr1, int *result, int num[], int n, int *total_perg,int *certas,int *erradas)
    Why i can't use the same process used in function2??
    I have a warnig saying
    expexts %d but argument 2 is int*

    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I have a warnig saying
    expexts %d but argument 2 is int*
    Copy and paste the actual warning text from the compiler.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Hi
    Thanks for your help

    in function 3
    D:\2012\c_exe\Projeto Final\funcoes.c|876|warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat]|
    in line
    printf ("\n You bring from the other stage: %d points \n", result); //counts the total amount of points earned
    Last edited by Gil Carvalho; 06-17-2012 at 11:08 AM.

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    printf ("\n You bring from the other stage: %d points \n", *result);
    Try dereferencing result.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Well;

    This is my funtion nº3

    Code:
    int gp3_Ques(char *usr1, int result, int num[], int n, int *total_perg,int *certas,int *erradas)
    {
    
    
        system("cls");
        printf ("\n*=============================================================================*\n");
        printf ("|                           >>>>Ordenacao numeral<<<<                            |\n");
        printf ("*=============================================================================*\n");
        printf ("                                                                               \n");
        printf ("\n      User %s \n",usr1);
        printf ("\n      Brings: %d points from last stage \n", result); //conta as perguntas
        printf ("\n      Full score in this satge will be: 2 \n"); //pontuação máxima do jogo
        printf ("                                                                               \n");
        printf ("*=============================================================================*\n");
        Sleep(3000);
        if ( jogo1Gp3(num,n) )
                {
                    printf("\nGood answer...\n");
                    result++;//incrementa a variavel result
                    total_perg++;
                    certas++;
                    printf("Next question...\n");
                    Sleep(2000);
                    if ( jogo1Gp3(num,n))
                    {
                        printf("\nGood answer...\n");
                        result++; //incrementa a variavel result
                        total_perg++;
                        certas++;
                    }
                    else
                    {
                        printf("Wrong answer.... :(");
                        total_perg++;
                        erradas++;
                        Sleep(2000);
                        system("cls");
                    }
                }
                else
                {
                    printf("\nWrong asnwer....you are out :(");
                    total_perg++;
                    erradas++;
                    Sleep(2000);
                    system("cls");
                    jogo();
                }
        if (result>=7)
        {
            printf ("\n*=============================================================================*\n");
            printf ("|                       >>>>Resultado desta fase 3<<<<                        |\n");
            printf ("*=============================================================================*\n");
            printf ("                                                                               \n");
            printf("\nTotal de %d points\n", result);
            printf("\nTotal de %d questions\n", total_perg);
            printf("\n %d de good answers.\n", certas);
            printf("\nTem %d wrong answers.\n", erradas);
            printf("\n                        Next stage 4...aguarde \n");
            printf ("*=============================================================================*\n");
            Sleep(5000);
            //gp3_Ques(usr1, &result);
    
    
        }
        else
        {
            printf("You didn't earn minimum poinst to go to the next stage....");
            Sleep(2000);
            system("cls");
            jogo();
        }
        return 0;
    }
    I have warning in
    format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat]|
    Code:
    printf("\nTotal de %d points\n", result);
    printf("\nTotal de %d questions\n", total_perg);
    printf("\n %d de good answers.\n", certas);
    printf("\nTem %d wrong answers.\n", erradas);
    If i put * the program crash, if i let that way, he gives me strange numbers

    printf("\nTem %d wrong answers.\n", erradas); gave me 2293700 instead of 0

  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Erradas needs to be dereferenced, otherwise your printing the address of the variable. Remember that the name of the pointer variable is the address that it holds. Anytime you want to make actual changes to what it points to then you have to use the '*'.
    Last edited by camel-man; 06-17-2012 at 11:50 AM.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    With deferencing.
    Code:
    printf("\nTem %d wrong answers.\n", *erradas);
    printf("\nTem %d wrong answers.\n", *erradas); gave me -1

    Don't get what's wrong

  8. #8
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You also need to derefernece it whenever you are making changes to it for example you say Estradas++.... when it should be (*Estradas)++;
    How are you calling your function in main?

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    In main the function is called like this

    Code:
    gp3_Ques(usr1, *result, *total_perg, *certas, *erradas);

  10. #10
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    you shouldnt call it like that, if your passing a pointer you just need to pass the address of the variable or the actual pointer itself depending on how you are doing your program.

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    So i use it like this?

    Code:
    gp3_Ques(usr1, &result, &total_perg, &certas, &erradas);
    And then i use deferecing in all my variables like this:
    Code:
    printf("\nTotal de %d points\n", *result); printf("\nTotal de %d questions\n", *total_perg); printf("\n %d de good answers.\n", *certas); printf("\nTem %d wrong answers.\n", *erradas);
    Is this ok??

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Your function definition:
    Code:
    int gp3_Ques(char *usr1, int result, int num[], int n, int *total_perg,int *certas,int *erradas)
    Your call:
    Code:
    gp3_Ques(usr1, *result, *total_perg, *certas, *erradas);
    Do you see your problems? (Hint: look at the number and type of the arguments)

    Bye, Andreas

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Gil Carvalho View Post
    So i use it like this?

    Code:
    gp3_Ques(usr1, &result, &total_perg, &certas, &erradas);
    And then i use deferecing in all my variables like this:
    Code:
    printf("\nTotal de %d points\n", *result); printf("\nTotal de %d questions\n", *total_perg); printf("\n %d de good answers.\n", *certas); printf("\nTem %d wrong answers.\n", *erradas);
    Is this ok??
    I have a pointer declared like this:
    int intNumber = 3;
    int *p = &intNumber;

    The expression p in code is the pointer's value, an address.
    The expression *p in code is the intNumber object.

    Based on what you've learned, what is the type of each expression in your question, and is it ok?

  14. #14
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    p alone is a variable

    *p is a pointer and gives the contente of an objet

    and & gives the adress of a variable

    right?


    AndiPersti

    The order is important??

    Iv'e changed to

    Code:
    
    
    Code:
    int gp3_Ques(char *usr1, int *result,int *total_perg,int *certas,int *erradas, int num[], int n)
    


    it is ok?
    Last edited by Gil Carvalho; 06-17-2012 at 12:44 PM.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >> p alone is a variable
    Yes, but that is not useful information. p is a variable, and so is *p, and they are completely different types.

    >> *p is a pointer and gives the contente of an objet
    *p is the type of the object it points to, and will yield a value of that type. In the example I posted, *p is an int. *p is not necessarily a pointer. It really depends on the type of p what type *p will yield. As you add asterisks (*) to p's declaration, the use of p gets more complicated, and the number of layers of pointers increases. For example int ***p; is a very complicated pointer with three layers and it's used by three star programmers.

    It's also apparent from the declaration that ***p is an int.

    >> and & gives the adress of a variable
    Yes, exactly.
    Last edited by whiteflags; 06-17-2012 at 12:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubt if this is bad pointer
    By gusth in forum C Programming
    Replies: 2
    Last Post: 09-08-2011, 12:24 AM
  2. pointer doubt....
    By roaan in forum C Programming
    Replies: 14
    Last Post: 07-29-2009, 11:20 AM
  3. Doubt in pointer.
    By shwetha_siddu in forum C Programming
    Replies: 5
    Last Post: 03-21-2009, 01:28 AM
  4. Doubt regarding pointer
    By karthik537 in forum C Programming
    Replies: 7
    Last Post: 01-21-2009, 09:53 AM
  5. A doubt regarding FILE * pointer
    By rohan_ak1 in forum C Programming
    Replies: 4
    Last Post: 08-01-2008, 05:15 AM