jayanth.devarayanadurga
Banned
- Joined
- Dec 4, 2012
- Messages
- 4,280
- Helped
- 822
- Reputation
- 1,654
- Reaction score
- 791
- Trophy points
- 1,393
- Location
- Bangalore, India
- Activity points
- 0
Pointers are very easy.
You define a ordinary variable this way
var_type var_name
You define Pointers this way
var_type *var_name
Eg: a pointer to int type is declared as
int *ptrCount
If count is a int type variable then you can get or set the value of Count indirectly by using pointer (like *ptrCount)
To do this you have to assign the address of Count to ptrCount and you do this like
ptrCount = &Count;
& before Count is the addressOf operator and it gets the address of the variable Count and assignes it to the pointer (pointer variable) ptrCount
Both Count and ptrCount have right-value and left-value.
Right value is the value the variable contains
Left value is the address of the variables in memory.
A pointers right value is address of the variable to which it is goint to point to.
If value od Count is 10 and its address is 0x1234 and address of ptrCount is 0x4321 then by doing
ptrCount = &Count;
you are setting the value of ptrCount to 0x1234
If you want to get or set the value of Count using ptrCount then you have to use dereferencing or indirection operator *
If you have another int type variable named result and want to assign the value of Count to it then you do like this
result = *ptrCount;
It means assign to result the value (right value of Count) the pointer ptrCount is pointing to.
ptrCount is pointing to Count and Count contains 10. So, 10 is assigned to result.
To set value of Count indirectly you do like this...
If you want to increase the value of Count to 20 then...
a) Count = *ptrCount + 10;
b) or Count = *ptrCount * 10;
a means add 10 to the value to which ptrCount is pointing to and assign it to Count.
b means multiply the value to which the pointer ptrCount is pointing to by 2 and assign the new value to Count.
You define a ordinary variable this way
var_type var_name
You define Pointers this way
var_type *var_name
Eg: a pointer to int type is declared as
int *ptrCount
If count is a int type variable then you can get or set the value of Count indirectly by using pointer (like *ptrCount)
To do this you have to assign the address of Count to ptrCount and you do this like
ptrCount = &Count;
& before Count is the addressOf operator and it gets the address of the variable Count and assignes it to the pointer (pointer variable) ptrCount
Both Count and ptrCount have right-value and left-value.
Right value is the value the variable contains
Left value is the address of the variables in memory.
A pointers right value is address of the variable to which it is goint to point to.
If value od Count is 10 and its address is 0x1234 and address of ptrCount is 0x4321 then by doing
ptrCount = &Count;
you are setting the value of ptrCount to 0x1234
If you want to get or set the value of Count using ptrCount then you have to use dereferencing or indirection operator *
If you have another int type variable named result and want to assign the value of Count to it then you do like this
result = *ptrCount;
It means assign to result the value (right value of Count) the pointer ptrCount is pointing to.
ptrCount is pointing to Count and Count contains 10. So, 10 is assigned to result.
To set value of Count indirectly you do like this...
If you want to increase the value of Count to 20 then...
a) Count = *ptrCount + 10;
b) or Count = *ptrCount * 10;
a means add 10 to the value to which ptrCount is pointing to and assign it to Count.
b means multiply the value to which the pointer ptrCount is pointing to by 2 and assign the new value to Count.