Pointers

This is a discussion on Pointers within the C++ Programming forums, part of the General Programming Boards category; Hey, I've been playing around with C++ for a few weeks however I seem to be doing something wrong with ...

  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,245
    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%.

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

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    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, 06: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, 10:25 PM
  5. Pointers pointers pointers....
    By G'n'R in forum C Programming
    Replies: 11
    Last Post: 11-02-2001, 01:02 AM

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