So do you think it will be more eficient if i use:
And then use inCode:int gp3_Ques(char *usr1, int result,int total_perg,int certas,int erradas, int num[], int n)
printf result, etc
So do you think it will be more eficient if i use:
And then use inCode:int gp3_Ques(char *usr1, int result,int total_perg,int certas,int erradas, int num[], int n)
printf result, etc
Last edited by Gil Carvalho; 06-17-2012 at 01:01 PM.
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.
>> 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.
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.
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:
since i call this function like thisCode:int gp3_Ques(char *usr1, int *result,int *total_perg,int *certas,int *erradas, int num[], int n)
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?Code:gp3_Ques(usr1, &result, &total_perg, &certas, &erradas);
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.
And how do you call this new function? Do you still call it without values for "num[]" and "n"?Iv'e changed to
Code:int gp3_Ques(char *usr1, int *result,int *total_perg,int *certas,int *erradas, int num[], int n)
BTW: Doesn't your compiler tell you about the missing arguments?
Bye, Andreas
No Andreas
"num[]" and "n"? are used
if ( jogo1Gp3(num,n) ) this call another function who randomize an array. (is working)
Hi Cristop
My code is this one
Code:gp3_Ques(usr1, &result, &total_perg, &certas, &erradas);All the warnings are when i want to print a variableCode: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; }
warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat]|
I have already tried several ways...
>> 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.
Is this the current prototype for gp2_Ques?
If so, what type is the expression &result?Code:int gp2_Ques(struct gp2 *p, char *usr1, int *result,int *total_perg,int *certas,int *erradas);
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.
Sorry christop
my current prototype is
Compiler warningsCode:gp3_Ques(usr1, &result, &total_perg, &certas, &erradas);
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 ===|
Well, i think is better to put all my functions and prototypes.
Maybe there is something wrong
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.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)
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: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)
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://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)
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
Ok, thanks
Iv'e made some changes like you told me.
Now between function 1 and 2 i don't have user , but i can't take it out, it's no necessaryCode: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)
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 ===|