Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++Pointer
This question is boggling my curiosity for the past day and a half.
I've put these two lines to test out explicit assignment of memory
addresses to pointers.
 
char * ptr2;
ptr2=(char*)0xB0110000 ;
 
But every time compiler thinks i'm crazy:
 
1>------ Build started: Project: AFF_CPLUS_01, Configuration: Debug Win32 ------
1>  ChildsPlay.cpp
1>c:\users\grays4nd\documents\visual studio 2012\projects\aff_cplus_01\aff_cplus_01\childsplay.cpp(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\grays4nd\documents\visual studio 2012\projects\aff_cplus_01\aff_cplus_01\childsplay.cpp(7): error C2040: 'ptr2' : 'int' differs in levels of indirection from 'char *'
1>c:\users\grays4nd\documents\visual studio 2012\projects\aff_cplus_01\aff_cplus_01\childsplay.cpp(7): error C2440: 'initializing' : cannot convert from 'char *' to 'int'
1>          There is no context in which this conversion is possible
1>c:\users\grays4nd\documents\visual studio 2012\projects\aff_cplus_01\aff_cplus_01\childsplay.cpp(14): error C2088: '<<' : illegal for class
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
 
When I try the same shenanigans inside of main() or other function, the compiler has no problem, only global assignment seems to present an issue. What gives?
Posted 18-Jan-13 19:30pm
Edited 18-Jan-13 19:42pm
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The first line is a declaration, the second line is an assignment.
 
The compiler only expects declarations at file scope, so you get the "missing type specifier" error for the second line and the other errors follow on from that.
 
If you really want to set the pointer outside of the functions, do it all on one line:
char *ptr2 = (char*)0xB0110000;
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 540
1 Tadit Dash 305
2 Sergey Alexandrovich Kryukov 250
3 Peter Leow 190
4 OriginalGriff 148
0 Sergey Alexandrovich Kryukov 9,395
1 OriginalGriff 5,473
2 Peter Leow 4,150
3 Maciej Los 3,540
4 Abhinav S 3,333


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 19 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid