How does pointer type casting work in c -


i understand in c, pointer points memory address. in following code

char *cp; sometype *up; // assuming sometype union of size 16 bytes cp = sbrk(nu * sizeof(sometype));   // here confuses me = (sometype *)cp; 

according link http://en.wikibooks.org/wiki/c_programming/pointers_and_arrays

  1. if cp points address of 0x1234, 0x1234 should beginning of newly allocated memory, right?

  2. so when "cp" casted pointer sometype , assigned "up", says "up" pointer points 0x1234, assumes in 32-bits system, each memory address takes 4 bytes, sometype object use 4 memory address store value, addresses 0x1234, 0x1235, 0x1236, 0x1237 collectively store sometype object, correct?

thanks in advance.

pointer type casting doesn't @ machine level. value in memory still plain old value in memory.

at language level, value used address. in other words used value passed operations require memory locations, such assembly "load" operation.

at language level, constructs added machine operations, , 1 of "extras" data types. compiler responsible checking if type-rule violations exist, @ run time there not such constraint.

as result, cast nothing @ run time, @ compile time directs compiler not emit error type of pointer's value considered compatible type variable holding address.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -