Thread: Pointer doubt

  1. #16
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    So do you think it will be more eficient if i use:

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

    printf result, etc
    Last edited by Gil Carvalho; 06-17-2012 at 01:01 PM.

  2. #17
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    No, because result is an int now and does not need to be dereferenced. In this case you should just use result with out '*'. However, there will be no change to the variable back in main, once your function ends all arithmetic on variables will be useless without the presence of some pointers. It depends on your program whether or not a function like that will suffice.

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> So do you think it will be more eficient if i use:
    No I don't think it would be more efficient.

    But I do think you should know independently if your usage of pointers is ok or not. You will have to master them.

  4. #19
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    Quote Originally Posted by Gil Carvalho View Post
    So do you think it will be more eficient if i use:
    Efficiency should be the last thing on your mind. Correctness should be first. Othewise you will have a program that efficiently crashes or gives you wrong results.

    Edit: your compiler will tell you (if you turn on warnings, which everyone should do already) when you're using pointers incorrectly. If you don't understand what the errors/warnings mean, copy and paste them here, along with the exact source code that gave you the errors.
    Last edited by christop; 06-17-2012 at 01:16 PM.

  5. #20
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    I don't need to update variable to main since this is a game with 4 stages, i just need to count the result, questions, wrong questions, etc

    I'm keeping the pointers like this:
    Code:
    int gp3_Ques(char *usr1, int *result,int *total_perg,int *certas,int *erradas, int num[], int n)
    since i call this function like this
    Code:
    gp3_Ques(usr1, &result, &total_perg, &certas, &erradas);
    So if i understand, when i call the function i'm using & with memory adress, then i use in function prototype * to deferencing the variables, right?

  6. #21
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Gil Carvalho View Post

    The order is important??
    Of course it is important!
    Seriously, have you ever worked through a tutorial/book? Because I have the impression you don't understand some very fundamental programming concepts.

    Iv'e changed to
    Code:
    int gp3_Ques(char *usr1, int *result,int *total_perg,int *certas,int *erradas, int num[], int n)
    And how do you call this new function? Do you still call it without values for "num[]" and "n"?

    BTW: Doesn't your compiler tell you about the missing arguments?

    Bye, Andreas

  7. #22
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    No Andreas

    "num[]" and "n"? are used

    if ( jogo1Gp3(num,n) ) this call another function who randomize an array. (is working)

  8. #23
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Hi Cristop

    My code is this one

    Code:
    gp3_Ques(usr1, &result, &total_perg, &certas, &erradas);
    Code:
    int gp3_Ques(char *usr1, int *result,int *total_perg,int *certas,int *erradas, int num[], int n)
    {
    
    
        system("cls");
    
    
        printf ("\n*=============================================================================*\n");
        printf ("|                           >>>>Ordenacao numeral<<<<                            |\n");
        printf ("*=============================================================================*\n");
        printf ("                                                                               \n");
        printf ("\n      Esta no bom caminho %s \n",usr1);
        printf ("\n      Traz da outra fase: %d pontos \n", result); //conta as perguntas
        printf ("\n      O seu score maximo sera: 2 \n"); //pontuação máxima do jogo
        printf ("                                                                               \n");
        printf ("*=============================================================================*\n");
        Sleep(3000);
        if ( jogo1Gp3(num,n) )
                {
                    printf("\nAcertou na resposta...\n");
                    result++;//incrementa a variavel result
                    total_perg++;
                    certas++;
                    printf("Atencao a proxima pergunta...\n");
                    Sleep(2000);
                    if ( jogo1Gp3(num,n))
                    {
                        printf("\nAcertou novamente na resposta...\n");
                        result++; //incrementa a variavel result
                        total_perg++;
                        certas++;
                    }
                    else
                    {
                        printf("Resposta errada.... :(");
                        total_perg++;
                        erradas++;
                        Sleep(2000);
                        system("cls");
                    }
                }
                else
                {
                    printf("\nResposta errada....foi eliminado :(");
                    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("\nTem um total de %d pontos\n", result);
            printf("\nTem um total de %d perguntas\n", total_perg);
            printf("\nTem %d de respostas certas.\n", certas);
            printf("\nTem %d de respostas erradas.\n", erradas);
            printf("\n                        Vai passar para a fase 4...aguarde \n");
            printf ("*=============================================================================*\n");
            Sleep(5000);
            //gp3_Ques(usr1, &result);
        }
        else
        {
            printf("Nao atingiu o minimo para seguir....vai voltar ao menu anterior");
            Sleep(2000);
            system("cls");
            jogo();
        }
        return 0;
    }
    All the warnings are when i want to print a variable

    warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat]|

    I have already tried several ways...

  9. #24
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat]|

    Right, so you want to print an integer. Pointer p points to an integer. What expression involving p did you learn that is of type integer?

    ... *p
    So use that information. I was hoping you would learn it, but I just wound up disappointed in myself.

  10. #25
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    Is this the current prototype for gp2_Ques?
    Code:
    int gp2_Ques(struct gp2 *p, char *usr1, int *result,int *total_perg,int *certas,int *erradas);
    If so, what type is the expression &result?

    It's not a pointer to int. It's a pointer to a pointer to int (int **). So you're passing the wrong thing to gp3_Ques.

    Again, turn on your compiler warnings, and pay attention to them. I'm pretty sure your compiler warns you that you're passing an int ** to the function gp3_Ques where an int * is expected.

  11. #26
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Sorry christop

    my current prototype is

    Code:
     gp3_Ques(usr1, &result, &total_perg, &certas, &erradas);
    Compiler warnings



    D:\2012\c_exe\Projeto Final\funcoes.c|901|warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat]|
    D:\2012\c_exe\Projeto Final\funcoes.c|939|warning: comparison between pointer and integer [enabled by default]|
    D:\2012\c_exe\Projeto Final\funcoes.c|945|warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat]|
    D:\2012\c_exe\Projeto Final\funcoes.c|946|warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat]|
    D:\2012\c_exe\Projeto Final\funcoes.c|947|warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat]|
    D:\2012\c_exe\Projeto Final\funcoes.c|948|warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat]|
    ||Warning: resolving _Sleep by linking to _Sleep@4|
    ||=== Build finished: 0 errors, 6 warnings ===|

  12. #27
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Gil Carvalho View Post
    my current prototype is
    Code:
     gp3_Ques(usr1, &result, &total_perg, &certas, &erradas);
    That's not a prototype, that's a function call.

    Bye, Andreas

  13. #28
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Well, i think is better to put all my functions and prototypes.
    Maybe there is something wrong
    Code:
    int gp1_Ques(struct perg *p, char *usr1)
    //function call
    gp2_Ques(&p, usr1, &result, &total_perg, &certas, &erradas);
    
    //function2
    int gp2_Ques(struct gp2 *p, char *usr1, int *result,int *total_perg,int *certas,int *erradas)
    
    //Function call
    gp3_Ques(usr1, &result, &total_perg, &certas, &erradas);
    
    
    //Fucntion 3
    int gp3_Ques(char *usr1, int *result,int *total_perg,int *certas,int *erradas, int num[], int n)
    I feel that all of you thinks that i'm dummie but my doubt is why with function 2 i use printf *result, and i can't use in function 3 the same.

  14. #29
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    int gp1_Ques(struct perg *p, char *usr1)
    //function call
    gp2_Ques(&p, usr1, &result, &total_perg, &certas, &erradas);
    
    //function2
    int gp2_Ques(struct gp2 *p, char *usr1, int *result,int *total_perg,int *certas,int *erradas)
    p is a pointer to struct perg. Then you call gp2_Ques with &p (which is now a pointer to a pointer to struct perg) but gp2_Ques needs a pointer to struct gp2.


    Code:
    //function2
    int gp2_Ques(struct gp2 *p, char *usr1, int *result,int *total_perg,int *certas,int *erradas)
    
    //Function call
    gp3_Ques(usr1, &result, &total_perg, &certas, &erradas);
    
    //Fucntion 3
    int gp3_Ques(char *usr1, int *result,int *total_perg,int *certas,int *erradas, int num[], int n)
    Here you have the same problem as above. result, total_perg, certas, erradas are already pointers to int. But with your call to gp3_Ques you make pointers to pointers. And you don't call gp3_Ques with values for num[] and n. So you should get an error from the compiler:
    Code:
    $ cat test.c
    #include <stdio.h>
    
    void f(int a, int b)
    {
        printf("%d %d\n", a, b);
    }
    
    int main(void)
    {
        f(1);
    
        return 0;
    } 
    $ gcc -Wall -o test test.c
    test.c: In function ‘main’:
    test.c:10:5: error: too few arguments to function ‘f’
    test.c:3:6: note: declared here

  15. #30
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Ok, thanks

    Iv'e made some changes like you told me.
    Code:
    intgp1_Ques(structperg *p, char*usr1)
    //function call gp2_Ques(usr1, &result, &total_perg, &certas, &erradas); //Function int gp2_Ques(struct gp2 *p, char *usr1, int *result,int *total_perg,int *certas,int *erradas) //function call gp3_Ques(usr1, result, total_perg, certas, erradas); //Function int gp3_Ques(char *usr1, int *result,int *total_perg,int *certas,int *erradas)
    Now between function 1 and 2 i don't have user , but i can't take it out, it's no necessary

    D:\2012\c_exe\Projeto Final\funcoes.c||In function 'gp3_Ques':|
    D:\2012\c_exe\Projeto Final\funcoes.c|938|warning: comparison between pointer and integer [enabled by default]|
    D:\2012\c_exe\Projeto Final\funcoes.c|944|warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat]|
    D:\2012\c_exe\Projeto Final\funcoes.c|945|warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat]|
    D:\2012\c_exe\Projeto Final\funcoes.c|946|warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat]|
    D:\2012\c_exe\Projeto Final\funcoes.c|947|warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat]|
    ||Warning: resolving _Sleep by linking to _Sleep@4|
    ||=== Build finished: 0 errors, 5 warnings ===|

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