Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. pointer question

pointer question

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingperformancehelpquestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    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..

    B T A H 4 Replies Last reply
    0
    • A Anonymous

      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..

      B Offline
      B Offline
      Budric B
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • B Budric B

        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.

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        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

        T B 2 Replies Last reply
        0
        • A Anonymous

          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

          T Offline
          T Offline
          TFrancis
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • A Anonymous

            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

            B Offline
            B Offline
            Budric B
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • A Anonymous

              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..

              T Offline
              T Offline
              Tyrus182
              wrote on last edited by
              #6

              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 }

              1 Reply Last reply
              0
              • A Anonymous

                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..

                A Offline
                A Offline
                Anonymous
                wrote on last edited by
                #7

                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....

                1 Reply Last reply
                0
                • A Anonymous

                  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..

                  H Offline
                  H Offline
                  Henry miller
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups