Like Tree1Likes
  • 1 Post By whiteflags

Compiler Showing Errors When Getting Address of a Variable?

This is a discussion on Compiler Showing Errors When Getting Address of a Variable? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> using namespace std; int main() { int x; int p_x = & x; *p_x = ...

  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
    22,313
    You forgot to declare p_x to be a pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,763
    This thread may be of interest. Jumping into C++ review - part 1 Make sure the error hasn't already been found.
    Swadesh likes this.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    22
    Quote Originally Posted by whiteflags View Post
    This thread may be of interest. Jumping into C++ review - part 1 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, 05:43 AM
  2. Replies: 9
    Last Post: 06-11-2012, 04: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, 05: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

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