Viral Parmar
Viral Parmar

Reputation: 31

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

Upvotes: 3

Views: 145

Answers (4)

Déjà vu
Déjà vu

Reputation: 28850

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...).

Upvotes: 1

sud03r
sud03r

Reputation: 19779

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.

Upvotes: 0

MattK
MattK

Reputation: 10293

It looks like the pointer, ch, is being assigned an absolute memory address 3000. Generally a very bad idea, unless you're working on an embedded system with no paging and you know exactly what's at memory location 3000.

Upvotes: 9

DevSolar
DevSolar

Reputation: 70391

Isn't it obvious?

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

Upvotes: 0

Related Questions