Thread: Need a bit of help with pointers

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    158

    Need a bit of help with pointers

    Code:
    int main()
      {
      int form;
      int x1,x2,y1,y2;
      double m,b;
     
      if(get_problem(form)==1)   //if get_problem return value is 1
      {
      get2_pt(&x1,&x2,&y1,&y2); //call the next function
      display2_pt(&x1,&x2,&y1,&y2);
      }
      return 0;
     }
     
      int get_problem(int form)
      {
      printf("Select the form that you like to convert to slope-intercept form:\n1    ) Two-point form (you know two points on the line)\n2) Point-slope form (you     know the line's slope and one point)\n=> ");
      scanf("%d",&form);
      return form;  //user either enters 1 or 2
      }
     
     
       void get2_pt(int *x1,int *x2, int *y1,int *y2) //potential problem source
      {
      printf("Enter the x-y coordinates of the first point seperated by a space=>      ");
      scanf("%d %d",&x1,&y1);
      printf("Enter the x-y coordinates of the second point seperated by a space=>      ");
     
    
    void display2_pt(int *x1, int *x2, int *y1, int *y2)
      {
      printf("Two-point form\n");
      printf("%d%d",x2,x1); //instead of printing out the value, it prints out the address.
    }
    My question is. Where did i go wrong? Print only prints only the addresses of the variable, and not the value

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, if you want to print the value, print *x2 and *x1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Hmm printf("%d%d",*x2,*x1);

    still prints out the addresses. This is the deref operator, correct?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tmac619619
    Hmm printf("%d%d",*x2,*x1);

    still prints out the addresses.
    No, it doesn't.

    Quote Originally Posted by tmac619619
    This is the deref operator, correct?
    Yes.

    Take a look at this line in the get2_pt function:
    Code:
    scanf("%d %d",&x1,&y1);
    x1 and y1 are pointers, so why do you take their address when you want to read into an int? You should have written:
    Code:
    scanf("%d %d", x1, y1);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Thank you based god!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers help please(using pointers to copy a string)
    By Shinzu911 in forum C Programming
    Replies: 1
    Last Post: 05-01-2012, 02:52 PM
  2. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  3. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  4. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM