Pointer Scale Factor and Increments :
Variables of different type will take different amount of memory. Like,
Data type Byte Bits
short int 2 16
unsigned short int 2 16
unsigned int 4 32
int 4 32
long int 4 32
signed char 1 08
unsigned char 1 08
float 4 32
double 8 64
long double 12 96
Depends on OS and hardware the number bytes required may get vary.
int a=10;
int *p=&a;
In the above two statements,memory address of a is stored into the pointer variable. Say variable 'a' is stored at 1030 memory address. Hence pointer 'p' holds 1030. If we increment value of pointer like
p++;
then pointer will point to next memory location 1034. We will be able to access value present at 1034. Similarly,
p=p+5;
makes pointer 'p' to point 1050(assuming 1030 as initial address) i.e. p=1030 + 5*4. This is the way how pointer get effected.
Variables of different type will take different amount of memory. Like,
Data type Byte Bits
short int 2 16
unsigned short int 2 16
unsigned int 4 32
int 4 32
long int 4 32
signed char 1 08
unsigned char 1 08
float 4 32
double 8 64
long double 12 96
Depends on OS and hardware the number bytes required may get vary.
int a=10;
int *p=&a;
In the above two statements,memory address of a is stored into the pointer variable. Say variable 'a' is stored at 1030 memory address. Hence pointer 'p' holds 1030. If we increment value of pointer like
p++;
then pointer will point to next memory location 1034. We will be able to access value present at 1034. Similarly,
p=p+5;
makes pointer 'p' to point 1050(assuming 1030 as initial address) i.e. p=1030 + 5*4. This is the way how pointer get effected.
p is an int* so p++ will take it to 1034 and not 1032. Please update it, people may get confused.
ReplyDeletethank u indicating my error it is corrected
Deletewhy unsigned int is taking 4 byte but only 16 bit hows it possible 4 byte =32 bit ...plz explain
ReplyDeleteThank u for indicating errors,satendra, there was typing mistake. It is corrected.
Delete