Thread: Compiler Showing Errors When Getting Address of a Variable?

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    22

    Thumbs up Compiler Showing Errors When Getting Address of a Variable?

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    
        int x;
        int p_x = & x;
        *p_x = 2;
    
    
        cin.ignore();
        cin.get();
    
        return 0;
    }
    Compiler errors:
    Code:
    Error	2	error C2100: illegal indirection	c:\users\swadesh\c++ programs\pointerz\pointerz\pointerz.cpp	11Error	1	error C2440: 'initializing' : cannot convert from 'int *' to 'int'	c:\users\swadesh\c++ programs\pointerz\pointerz\pointerz.cpp	10
    	3	IntelliSense: a value of type "int *" cannot be used to initialize an entity of type "int"	c:\users\swadesh\c++ programs\pointerz\pointerz\pointerz.cpp	10
    	4	IntelliSense: operand of '*' must be a pointer	c:\users\swadesh\c++ programs\pointerz\pointerz\pointerz.cpp	11

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot to declare p_x to be a pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    22
    Quote Originally Posted by laserlight View Post
    You forgot to declare p_x to be a pointer.
    Thanks, That solved it.
    However, "Alex Allain" in his C++ book wrote the following code without the declaration you mentioned.

    I guess he made mistake.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    If i were you i would notify him with an email about it

    probably a typo

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    This thread may be of interest. http://cboard.cprogramming.com/progr...-part-1-a.html Make sure the error hasn't already been found.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    22
    Quote Originally Posted by whiteflags View Post
    This thread may be of interest. http://cboard.cprogramming.com/progr...-part-1-a.html Make sure the error hasn't already been found.
    @ whileflags: Thanks for this, now I can follow the thread you gave to see the corrections.

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GDB Not Showing Variable value
    By nickman in forum Linux Programming
    Replies: 5
    Last Post: 09-21-2012, 04:43 AM
  2. Replies: 9
    Last Post: 06-11-2012, 03:45 AM
  3. Command Promt showing in window programming - Compiler dependent?
    By random_cpp_user in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2010, 04:24 AM
  4. c++ not showing a variable
    By Mythic Fr0st in forum C++ Programming
    Replies: 5
    Last Post: 01-16-2007, 11:05 PM
  5. Call by Reference/Address Errors - Please Help
    By Vireyda in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2004, 06:50 PM