pointer question
-
Hai, I'm new to programming...I have to calculate some values in a func and get those values outside the func.. ie i'm doing as below main() { float *val1 = NULL; float *val2 = NULL; ........ myfunc(&val1,&val2); myfunc2(*val1,*val2); ......... } int myfunc(float *V1,float *V2) { int cal1 = 10; ..... *V1 = cal1 / 2; ---> It does'nt give me any error,but when I debug it gives me access violation in memory 0x0000000 here... *V2 = cal1 / 5; return 0; } where am I going wrong... Thanks a lot..
-
Hai, I'm new to programming...I have to calculate some values in a func and get those values outside the func.. ie i'm doing as below main() { float *val1 = NULL; float *val2 = NULL; ........ myfunc(&val1,&val2); myfunc2(*val1,*val2); ......... } int myfunc(float *V1,float *V2) { int cal1 = 10; ..... *V1 = cal1 / 2; ---> It does'nt give me any error,but when I debug it gives me access violation in memory 0x0000000 here... *V2 = cal1 / 5; return 0; } where am I going wrong... Thanks a lot..
-
myfunc(&val1,&val2); means you are passing the address of val1. Val1 is a pointer. Try myfunc(val1, val2); Also you need to initialize val1 and val2 to point to something. I hope you did that in the .....part of your main.
-
No I did'nt initialize anything.. My basic idea is this..I have to compute the values of two float variables in myfunc1 and use those values in myfunc2.How should I do this.. Thanks a lot
int main() { float v1 = 0.0; float v2 = 0.0; .... MyFunc(v1,v2); ... return 0; } void MyFunc(float& v1, float& v2) { //Change value of v1 & v2 } "float&" versus just "float" means that you will be passing by reference, not by value. I suggest you look up by value and by reference in your text/reference book to get a better understanding of this concept.
-
No I did'nt initialize anything.. My basic idea is this..I have to compute the values of two float variables in myfunc1 and use those values in myfunc2.How should I do this.. Thanks a lot
1. You can use references. float val1, val2; myfunc1(val1,val2); myfunc2(val1,val2); ... void myfunc1(float & val1, float & val2) { val1 = 10/4; etc etc } myfunc2 stays the same. 2. If you want to use pointers float val1, val2; myfunc1(&val1, &val2); myfun2(val1,val2); your functions should remain the same.
-
Hai, I'm new to programming...I have to calculate some values in a func and get those values outside the func.. ie i'm doing as below main() { float *val1 = NULL; float *val2 = NULL; ........ myfunc(&val1,&val2); myfunc2(*val1,*val2); ......... } int myfunc(float *V1,float *V2) { int cal1 = 10; ..... *V1 = cal1 / 2; ---> It does'nt give me any error,but when I debug it gives me access violation in memory 0x0000000 here... *V2 = cal1 / 5; return 0; } where am I going wrong... Thanks a lot..
ok first off when passing a pointer as an argument like you have you dont need to do & or * just use the variable name as long as the types match. you also need to either assign your variable something other than NULL either assign another pointer to it or do a new or malloc wicherver you prefer to create your variable, dont forget to delete your variable when your done. btw that error your getting is that value becuase thats the same value as the pointer your passing NULL = 0x0000000
int main() { float *var = new float; float *var1 = new float; myfunc(var, var1); printf("%f, %f", var, var1); delete var; delete var1; } void myfunc(float *var, float *var1) { //do some crap }
-
Hai, I'm new to programming...I have to calculate some values in a func and get those values outside the func.. ie i'm doing as below main() { float *val1 = NULL; float *val2 = NULL; ........ myfunc(&val1,&val2); myfunc2(*val1,*val2); ......... } int myfunc(float *V1,float *V2) { int cal1 = 10; ..... *V1 = cal1 / 2; ---> It does'nt give me any error,but when I debug it gives me access violation in memory 0x0000000 here... *V2 = cal1 / 5; return 0; } where am I going wrong... Thanks a lot..
main() { float val1 = 0.0; float val2 = 0.0; ........ myfunc(&val1, &val2); } You need to allocate some memory for Val1 and Val2. What you did allocated memory for 2 variables that were capable of storing the memory address of Val1 and Val2, but not the values themselves. For instance: int i; //declares that i is an int and allocates memory int *j; // declares that j can store a memory address to a location in memory that can store an //integer - but note, no memory has been allocated for that integer here j= &i; //assign j (capable of storing an address) the address of i (which in turn can store an int) Hope this helps you....
-
Hai, I'm new to programming...I have to calculate some values in a func and get those values outside the func.. ie i'm doing as below main() { float *val1 = NULL; float *val2 = NULL; ........ myfunc(&val1,&val2); myfunc2(*val1,*val2); ......... } int myfunc(float *V1,float *V2) { int cal1 = 10; ..... *V1 = cal1 / 2; ---> It does'nt give me any error,but when I debug it gives me access violation in memory 0x0000000 here... *V2 = cal1 / 5; return 0; } where am I going wrong... Thanks a lot..
The other guys are right. I just want to add some comments. Pointers are hard for novices. They seperate the experts from the novices! Don't just fix the code, spend some time to understand what is wrong. User pencil and paper if you must, and trace out exactly what is going on. It will be helpful to lookup handles as well. What you have done is created a handle (which is a topic even more advanced than pointers), so you will want to see what you have done.