7. Address Typecasting #

Created Tuesday 24 December 2019

Q) Why don’t we use pointer instead of int*, char* etc. A) We use int* and char* etc to:

Note: We do have generic pointers(i.e void pointers), but they cannot be derefenced. They need to be cast a data type before dereference.

Q) If a character pointer is assigned the address of an integer with value 65. Is this assignment(of address allowed)? A) Generally this is not valid for addresses. As the lvalue or rvalue jump(depending) on the type may give weird results or out of range behavior.

char c = ‘a’ int x = (int) &c; Note: when assigning an address to a void pointer, no type-casting is required. Q) Can we assign an a pointer of data_type1, a address of a diffrent type of variable? A) For integer values, most complilers store octets in the reverse order, called the Little Endian System. So 65 is stored as 65 0 0 0. In the Little Endian systemW, the :

  1. Last bit of the first octet is the LSB.
  2. First Bit of the last octet is the MSB(aka the sign bit).
  3. The second octet(from the left in the memory) is the 2nd last octet as written on paper. Similarly for other octets, i.e in the reverse order.

Big Endian stores the number as it is written on a paper(basic math), i.e MSB is the leftmost bit, LSB is the rightmost bit.