Thread: trouble

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    47

    Post trouble

    i have a code that i want to ask you for a number than give you the pointer to the number and then ask you what you want the pointer to be changed to and it dosen work it wont compile without an error message

    Code:
    #include <iostream>
    
    using namespace std;
    
    int times,number,pointer;
    int *p;
    int main()
    {
        cout<<"how many time will we make a pointer?"<<endl;
        cin>> times;
        while( times >= 0 )
        {
               cout<<"I need a number:"<<endl;
               cin>> number;
               p = &number;
               cout<<"The ponter of "<<number<<" is: "<< p<<endl;
               cout<<"What will its new pointer be?"<<endl;
               cin>> *p;
               number = p;
               cout<<"the pointer of number is now "<< *number<<endl;
               times --;
        }
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    > number = p;

    That is assigning a pointer value to an int variable. You probably mean this:

    number = *p;

    However, that is unnecessary because number and *p are always the same as long as p points to the address of number.

    In addition, when you output number, you don't need to use *number. It is just an int variable. I think you are getting confused between which variable is an int and which is a pointer.

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    you need to dereference the pointer here:
    Code:
    cout<<"The ponter of "<<number<<" is: "<< *p<<endl;
    and here
    Code:
    number = *p;
    However this last line is unecessary because
    Code:
    cin>> *p;
    already changed number.
    and finally, you can't dereference a variable, I would had thought you'd get an error
    Code:
    cout<<"the pointer of number is now "<< number << endl
    Now, I have a funny feeling you are trying to change the address of the pointer, if that is the case, don't, especially not by typing in some value willy nilly, you are asking for trouble. especially since you are then changing that value with the next cin. Big trouble!!

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    47
    ok now i know how to change the pointer of a number and i know how to use a pointer but y would i need to know how to do this is ther any point to haveing many vareibles with the same pointer?

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    ...is ther any point to haveing many vareibles with the same pointer?
    Well, a pointer holds an address, and it can only point-to one variable at a time. When you define a variable, the compiler assigns an address to it. Each variable gets it's own address.

    But, say you have a character array (C-style string)... A pointer can point-to one character, then another.

    Or, you might have two different pointers which happen to be pointing to the same character in the string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM