Thread: Pointers

  1. #1
    Registered User
    Join Date
    May 2010
    Location
    Essex, UK
    Posts
    6

    Question Pointers

    Hey, I've been playing around with C++ for a few weeks however I seem to be doing something wrong with pointers still.

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    main(){
    int age;
    int *data;
    
    *data &= age;
    cout<<"How old are you?\n";
    cin>>*data;
    cin.get();
    cout<<"you are " <<*data<< " years old!";
    cin.get();
    cout<<"I repeat you are " <<age<< " years old!!";
    cin.get();
    }
    I wanted to see if it did actually store the data correctly in both the original integer and the pointer however it seems to only store the input in the pointer and the age simply displays a random big number. Could anyone 'point' me in the right direction here

    Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Change the line
    Code:
    *data &= age;
    to
    Code:
    data = &age;
    This will make the pointer data contain the address of age.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Essex, UK
    Posts
    6
    Ah yes, thanks for the help!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Main must return int. As it is, this code won't/shouldn't compile. Everything must have a return code in C++. Omitting it is illegal.
    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. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 07:28 AM
  3. pointers to constants and constant pointers
    By homeyg in forum C++ Programming
    Replies: 1
    Last Post: 06-18-2005, 12:02 AM
  4. Arrays, pointers and strings
    By Apropos in forum C++ Programming
    Replies: 12
    Last Post: 03-21-2005, 11:25 PM
  5. Pointers pointers pointers....
    By G'n'R in forum C Programming
    Replies: 11
    Last Post: 11-02-2001, 02:02 AM