简体   繁体   中英

char pointer initialization

I have come across a line that is given below

           char *ch=(char*)3000

I want to know the meaning of this line .....

它看起来像指针ch被分配了一个绝对的内存地址3000.一般来说这是一个非常糟糕的主意,除非你在没有分页的嵌入式系统上工作,而且你确切知道内存位置3000的内容。

Maybe seeing the rest of the code would be relevant...

That pointer could be relative to the segment in which it resides (on Intel processors). In this case the 3000 could be simply an index into that segment, defined earlier in the program, where we don't have the lines.

This depends upon the system architecture, the environment, the OS, the compiler, the rest of the code (and the programmer...).

Isn't it obvious?

The numerical value "3000" is cast to a char pointer, ie ch is initialized to memory address 3000 (decimal).

AFAIK, 3000 is no special address/value, and In most of the cases accessing it would result in segmentation fault or a garbage value.

If you see that in code, may be it is incorrectly used instead of a (void*), say in case of maps where you have key value pairs, the result may be cast into an integer in that case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM