Thread: A question regarding pointers

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    A question regarding pointers

    Hello.
    I wrote this program to test my (very basic, limited) understanding of C++ thus far. What I got is this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int mult ( int x, int y );
    
    int main()
    {
      int x;
      int *xp = new int;
      int y;
      int *yp = new int;
      int txy ;
      int ran ;
    
      xp = &x;
      yp = &y;
      ran = rand() ;
      cout<<"Write num\n";
      cin>>x;
      cin.ignore();
      cout<<"The num you wrote is "<<*xp<<"\n";
      cout<<"Write num\n";
      cin>>y;
      cin.ignore();
      cout<<"The num you wrote is "<<*yp<<"\n";
      txy = mult(x, y) ;
    
      if (txy>ran)
      {
          cout<<txy<<">"<<ran ;
       }
       else
       {
           cout<<txy<<"<"<<ran ;
       }
    
       cin.get() ;
       return 0 ;
       delete xp ;
       delete yp ;
       xp = 0 ;
       yp = 0 ;
    
    }
      int mult ( int x, int y )
    {
      return x * y;
    }
    The problem here is the line txy = mult(*xp, *yp) ;. I get an error stating that I'm not allowed to compare pointers with integers. However as far as I understand using a pointer in this manner (*xp) should be just like using the original integer no?

    Thanks for any answer!

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Moved following request

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    There is nothing wrong with calling mult() in that way. You should not be getting a compile error with that code.

    Now there is a problem with your code though. You are doing the following:
    Code:
    int *xp = new int; // This is fine, allocating memory for an integer
    xp = &x; // Bad!  You just reassigned the pointer from the memory you allocated for it, and pointed it at a local variable.
             // Did you mean to do this: *xp = x; ?
    delete xp; // You are now trying to delete the local variable instead of what you allocated.
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Lots of problems related to pointers lately. I urge you do a search on the boards to learn more about the nature of pointers.
    Basically, a pointer is a variable that holds a memory address. To access the address the pointer stores, you use it as normal. To access the contents the pointer points to, you use the derefernce operator *.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    there is a memory leak in your code i think

    you lost the first address aquired by xp and yp by doing this
    Code:
    xp = &x;
    yp = &y;
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, there is a memory leak and undefined behavior as pointed out by bithub.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM