全局范围内的显式指针地址分配 [英] Explicit Pointer Address Assgn at Global Scope

查看:71
本文介绍了全局范围内的显式指针地址分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题让我对过去一天半的好奇心感到难以理解。

我已经把这两行用于测试显式的内存分配

地址到指针。



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 ==========





当我在main()或其他函数中尝试相同的恶作剧时,编译器没有问题,只有全球任务似乎存在问题。给出了什么?



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?

推荐答案

第一行是声明,第二行是作业。



编译器只需要在文件范围内进行声明,因此第二行会出现缺少类型说明符错误,其他错误也是如此。



如果你我真的想把指针设置在函数之外,在一行上完成所有操作:

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;


这篇关于全局范围内的显式指针地址分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆