pointer ?

This is a discussion on pointer ? within the C++ Programming forums, part of the General Programming Boards category; I'm a beginner trying to get the concept of pointers Why won't this compile? [code] #include <iostream> #include <stdio.h> #include ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    25

    pointer ?

    I'm a beginner trying to get the concept of pointers
    Why won't this compile?
    [code]
    #include <iostream>
    #include <stdio.h>
    #include <conio.h> /* for getch() */
    using namespace std;
    int main()
    {
    int x;
    int *pointer; // Declares pointer
    cout<<"enter a # for x ";
    cin>>x; //Reads in x
    cin=*pointer; //Declares a pointer to x
    pointer=&x; //Read it, "pointer equals the address of x"
    cout<<*pointer; //Note the use of the * to output the actual number stored in x
    getch();
    return 0;
    }
    [code/]

    gets an error at cin = *pointer;
    saying that pointer is undeclared........??

    Thanks for any input........

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    You dont need the
    Code:
    cin = *pointer;
    part.

    By the way the '/' goes before the 'code' like this [ / code] (with no spaces of course).
    Why drink and drive when you can smoke and fly?

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    25

    Marcos

    Thanks Marcos!!
    Ugh! saw that my code tags didn't work when I saw the post..

    Ok I got ya...
    int *pointer // declares the pointer
    cin >>x // reads in the variable
    pointer = &x // assigns the pointer to the address of x

    That's clearer now.....
    cin = *pointer was also confusing me due to cin == what type???
    How would it know how much memory to reserve...int, char, etc.

    Would it be a fair statement to say that when working with pointers the type must be defined ??
    Thanks Teeyester

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Not sure what you were doing with cin=*pointer...If you are trying to get the user to input a value for *pointer you could use cin>>*pointer:
    Code:
    ...
    pointer=&x;
    cin>>*pointer; //pretty much the same as cin>>x
    Would it be a fair statement to say that when working with pointers the type must be defined ??
    Not sure what you mean by that either...but I think you can use void pointers if you aren't sure what type the pointer will be pointing to
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    25
    JaWIB
    Heh ! I'm not too sure either....I'm trying to get a grip on pointers & the code above wouldn't compile....the cin = * pointer didn't seem to fit the format for pointers in the books and Marcos was right ...it compiled fine removing that part.....I just got caught up looking at how the type of variable affects what you point at.........ie: is it 1 byte for a char, 2 bytes for an int, etc. and in the code above x is defined as an int, but I was getting an error on cin = *pointer........as undeclared........maybe it does == x, since cin >> x........but it's still a bad statement since cin by itself == what type??..........anyway .....onward!! thanks for your input
    Teeyester

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    You can edit your code tags.

    Ugh! saw that my code tags didn't work when I saw the post..
    Hey teeyester,
    FYI - You could go back and fix your code tags by clicking the EDIT tab. I'm NOT suggesting that you should at this point... just letting you know that you can edit. It seems like I'm always going back and fixin' somethin'.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    25
    DougDbug
    Thanks for the headsup................

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571

    Re: Marcos

    Originally posted by teeyester
    int *pointer // declares the pointer
    Yes.

    cin >>x // reads in the variable
    Yes.

    pointer = &x // assigns the pointer to the address of x
    You have this backwards -- it assigns the address of x to the pointer (the pointer is altered to point at x; x is not changed at all)

    cin = *pointer was also confusing me due to cin == what type???
    How would it know how much memory to reserve...int, char, etc.
    You can't use "cin =" for ANYTHING. "cin = *pointer" would try to assign the value of *pointer to the input stream cin, and how can you assign a number to a stream?

    cin is an input stream, to read from the stream you use "cin >>". It's legal to say "cin >> *pointer" *IF* pointer points to memory that is previously allocated.

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    25

    Talking

    Cat

    Ahhhh!
    cin is an input stream, to read from the stream you use "cin >>". It's legal to say "cin >> *pointer" *IF* pointer points to memory that is previously allocated.

    Thank you very much!!..........VERY Helpful forum
    THANKS all................Teeyester............

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  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