Click here to Skip to main content
12,510,824 members (51,147 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++
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
Updated 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
Top Experts
Last 24hrsThis month


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100