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. Passing by value and printing in function

Passing by value and printing in function

Scheduled Pinned Locked Moved C / C++ / MFC
help
3 Posts 3 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.
  • U Offline
    U Offline
    User 22775
    wrote on last edited by
    #1

    Hello, I have an assignment I am working on, I am stuck at a beggining point. Below is the code I am working with. This is pretty much what the professor gave us to start with, all I am trying to do right now is pass some values to the subroutine and then print them in the subroutine to confirm the values passed. We have to pass by value. I can print the values in the main fine, but then I always get zeroes in the subroutine. I have a lot more to do in the assignment, but have been stuck here. Any help would be greatly appreciated. Thanks, Paul unsigned long adddouble(unsigned long x, unsigned long y) { printf("%ld %ld \n", x, y); return(x+y); /* no interest at this time */ } main() { double d1, d2, *dp; unsigned long *longp1,*longp2, s; scanf("%lf %lf", &d1, &d2); /* get two input real numbers */ printf("%lf %lf \n", d1, d2); longp1 = (unsigned long *) &d1; longp2 = (unsigned long *) &d2; s = adddouble(*longp1,*longp2); }

    C J 2 Replies Last reply
    0
    • U User 22775

      Hello, I have an assignment I am working on, I am stuck at a beggining point. Below is the code I am working with. This is pretty much what the professor gave us to start with, all I am trying to do right now is pass some values to the subroutine and then print them in the subroutine to confirm the values passed. We have to pass by value. I can print the values in the main fine, but then I always get zeroes in the subroutine. I have a lot more to do in the assignment, but have been stuck here. Any help would be greatly appreciated. Thanks, Paul unsigned long adddouble(unsigned long x, unsigned long y) { printf("%ld %ld \n", x, y); return(x+y); /* no interest at this time */ } main() { double d1, d2, *dp; unsigned long *longp1,*longp2, s; scanf("%lf %lf", &d1, &d2); /* get two input real numbers */ printf("%lf %lf \n", d1, d2); longp1 = (unsigned long *) &d1; longp2 = (unsigned long *) &d2; s = adddouble(*longp1,*longp2); }

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      longp1 = (unsigned long *) &d1; longp2 = (unsigned long *) &d2; this is not good. what you're doing here is setting a pointer to a long to point to the place where a double lives. i don't really see why you need to use pointers for any of the longs. try it without. -c


      Image tools: ThumbNailer, Bobber, TIFFAssembler

      1 Reply Last reply
      0
      • U User 22775

        Hello, I have an assignment I am working on, I am stuck at a beggining point. Below is the code I am working with. This is pretty much what the professor gave us to start with, all I am trying to do right now is pass some values to the subroutine and then print them in the subroutine to confirm the values passed. We have to pass by value. I can print the values in the main fine, but then I always get zeroes in the subroutine. I have a lot more to do in the assignment, but have been stuck here. Any help would be greatly appreciated. Thanks, Paul unsigned long adddouble(unsigned long x, unsigned long y) { printf("%ld %ld \n", x, y); return(x+y); /* no interest at this time */ } main() { double d1, d2, *dp; unsigned long *longp1,*longp2, s; scanf("%lf %lf", &d1, &d2); /* get two input real numbers */ printf("%lf %lf \n", d1, d2); longp1 = (unsigned long *) &d1; longp2 = (unsigned long *) &d2; s = adddouble(*longp1,*longp2); }

        J Offline
        J Offline
        Jambolo
        wrote on last edited by
        #3

        Take it from a veteran... Avoid casting whenever possible, even when you know what you are doing. Casting has always been and will always be a potent source of bugs.

        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