tags:

views:

78

answers:

5

hi i have come across a line that is given below

           char *ch=(char*)3000

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

A: 

Isn't it obvious?

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

DevSolar
... and it's something you probably don't want to be doing.
gspr
A: 

It's stating assign the pointer a value of 3000. Hence, it's saying that the pointer should point to the memory address signified by the value of 3000. This dangerous.

wheaties
+9  A: 

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.

MattK
An anology to complement this answer: Think of your system memory as an enormous sequence of baskets -- several billions of them on a modern computer. Without knowing anything more about what's in the baskets, you're saying "ch should mean basket number three thousand". You can probably guess that this is not a good idea unless you have a very specific plan.
gspr
There is at least one case in the Win32 DDK where you are expected to cast an integer to a pointer depending on other arguments to the function you are calling. It made me scream every time I had to call it.
Matt Kane
Any kernel-space code would suffice to see this kind of thing often. However, decimal 3000 sure does look suspect.
DevSolar
Why the address is interpreted as "absolute"? Isn't that virtual address? How virtual address 0x3000 gets mapped to physical address is beyond the scope of the program, isn't it?
ArunSaha
ArunSaha, that's why I stipulated that the poster might be working on an embedded system with no paging. If that's the case, then the memory address is absolute. You are correct that if there's some kind of paging (including virtual memory) under the hood, then 3000 could mean just about anything.
MattK
A: 

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.

Neeraj
+1  A: 

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

ring0