help with pointers

This is a discussion on help with pointers within the C++ Programming forums, part of the General Programming Boards category; part of my software where i test certain crap out since im learning is about pointers. Well I think im ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    5

    help with pointers

    part of my software where i test certain crap out since im learning is about pointers.
    Well I think im doin it right but it gives me errors.

    Errors:
    Code:
    1>d:\users\ant1jr\documents\visual studio 2005\projects\test 1\test 1\test 1.cpp(106) : error C2101: '&' on constant
    1>d:\users\ant1jr\documents\visual studio 2005\projects\test 1\test 1\test 1.cpp(106) : error C2440: '=' : cannot convert from 'int' to 'int *'
    1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    1>d:\users\ant1jr\documents\visual studio 2005\projects\test 1\test 1\test 1.cpp(109) : error C2101: '&' on constant
    1>d:\users\ant1jr\documents\visual studio 2005\projects\test 1\test 1\test 1.cpp(109) : error C2440: '=' : cannot convert from 'int' to 'int *'
    1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    1>d:\users\ant1jr\documents\visual studio 2005\projects\test 1\test 1\test 1.cpp(112) : error C2101: '&' on constant
    1>d:\users\ant1jr\documents\visual studio 2005\projects\test 1\test 1\test 1.cpp(112) : error C2440: '=' : cannot convert from 'int' to 'int *'
    1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    Code:
    Code:
    void Pointers()
    {
    int *p;
    int 1;
    int 2;
    int 3;
    int I;
    
    cout<<"What do you want 1 to be?\n";
    cin>> 1;
    cin.ignore();
    cout<<"What do you want 2 to be?\n";
    cin>> 2;
    cin.ignore();
    cout<<"What do you want 3 to be?\n";
    cin>> 3;
    cin.ignore();
    cout<<"What do you want the pointer to point to?\n";
    cin>> I;
    cin.ignore();
    switch ( I ) {
            case 1:  
    			p = &1;
    			break;
    		case 2:
    			p = &2;
    			break;
    		case 3:
    			p = &3;
    			break;
        }
    }

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Numbers are not valid variable names. How would your compiler tell if you wanted the variable or the actual value 2?

    Edit: Also, assuming your code worked, if I entered "4" at the last question, then p could point to just about anything. Either reprompt the user for valid input, or use a default: and set it to something valid.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Always initalize a pointer on delcaration!!!!

    Code:
    int *p = NULL;
    I dont get what the code is trying to do, the indentation is poor. Could you post the whole program if possble?
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    doesn't look like it was meant to accomplish anything useful, he said he was just learning about pointers.
    add a letter before 1, 2 and 3 or change the names

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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