want to assign myself the adress of a pointer

This is a discussion on want to assign myself the adress of a pointer within the C++ Programming forums, part of the General Programming Boards category; I have the following code: Code: void new_reservation () { cout<<"Pick an available seat"<<endl; cin>>number_seat; number_seat=&seat; } I get the ...

  1. #1
    Unregistered
    Guest

    Exclamation want to assign myself the adress of a pointer

    I have the following code:
    Code:
    void new_reservation ()
    {
    	cout<<"Pick an available seat"<<endl;
    	cin>>number_seat;
    	number_seat=&seat;
    }
    I get the following error:
    C:\avion.cpp(42) : error C2440: '=' : cannot convert from 'int *' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    Error executing cl.exe.

    is there any way I can make this work?
    thx in advanced.

  2. #2
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    seat ?

    you never defined seat in the posted code..

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    I assume you've already had seat defined somewhere.

    However in the posted code, you're grabbing number_seat from the user input and then assigning number_seat to the address of seat. Thus the user input never had an effect on the variable at all. Post more code. Not sure what you're trying to accomplish here.

  4. #4
    Unregistered
    Guest
    int seat;

    there, i assigned it for u.

  5. #5
    Unregistered
    Guest
    I want the user to be able to give the adress. I'm making a program for seat reservations on a plane. For example, a person comes in and asks to reserve seat 5. I want the program to go to seat 5(which is the adress) and check to see if the value of it is either 1 or 0 (1 is occupied, 0 is empty). After I check the value i have this:

    Code:
    if (seat==0)
    	{
    		seat=1;
    		cout<<"You have successfully reserved the seat"<<endl;
    	}
    	else
    	{
    		cout<<"Sorry. This seat has already been taken"<<endl;
    	}

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    I agree with Cshot. I think you might have flipped the equation.

  7. #7
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    I'm still not sure why you have to grab the address of the variable seat. Wouldn't it be easier to use indexes into arrays?

    For example:

    #define EMPTY 0
    #define FULL 1

    int seat[NUMBER_OF_SEATS];
    ...

    cin >> number_seat;
    if(seat[number_seat] == EMPTY)
    {
    ...
    }
    else
    {
    ...
    }

  8. #8
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    huh

    Originally posted by Unregistered
    int seat;

    there, i assigned it for u.
    you never initialized it

    Don't play with me buddy

  9. #9
    Unregistered
    Guest
    Cshot. I know I can use an array and it would work perfect. I'm just messing with pointers and trying to see all the things I can get out of them.

    For the other guy:
    seat=0;

    there. initialized it for u.

  10. #10
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    I don't think this is the best example to try using pointers. When you declare seat, your system finds a spot in memory to store that variable. Thus, you cannot really change the address of seat, unless you're managing a block of memory. I think there is a confusion with your usage of the term "address". You can't have the user input any address and then you coming in messing with that location. It's protecting itself from you coming into random memory blocks and messing with it.

    Thus, my suggestion is to declare a block of memory and then use the user input as an offset into your block. You can then check to see the status of the seat by using the base address of your memory block and adding the offset to it. This is basically like an index into an array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assign a string to char pointer
    By seaking1 in forum C Programming
    Replies: 8
    Last Post: 12-08-2009, 01:54 PM
  2. Adress of a pointer.
    By ozumsafa in forum C Programming
    Replies: 2
    Last Post: 09-21-2007, 02:59 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21