C - Pointers
A pointer is a special type of variable which is used to store an address of an existing variable.
Now let's see how to use a pointer.
In the following example we have used a pointer to indirectly access a variable.
A pointer can be called in two different ways.
1. Call by value
2. Call by reference
Call by value
In call by value, the copy of values of the variables are passed to the functions.
In the following example copies of variables a and b are assigned to the variables x and y in the function swap.
The values does not change here.Because they are just variables not pointers.We can't swap the values of variables as above.
Call by Reference
In call by reference, the memory addresses are assigned to the pointers.Therefore it will permanently change the value after a function call.
In here values are changed because there are no variables but pointers.We can change the values of pointers simply as above.
In here I have given you a brief introduction to pointers just to educate you how pointers work.
Now let's see how to use a pointer.
In the following example we have used a pointer to indirectly access a variable.
A pointer can be called in two different ways.
1. Call by value
2. Call by reference
Call by value
In call by value, the copy of values of the variables are passed to the functions.
In the following example copies of variables a and b are assigned to the variables x and y in the function swap.
The values does not change here.Because they are just variables not pointers.We can't swap the values of variables as above.
Call by Reference
In call by reference, the memory addresses are assigned to the pointers.Therefore it will permanently change the value after a function call.
In here values are changed because there are no variables but pointers.We can change the values of pointers simply as above.
In here I have given you a brief introduction to pointers just to educate you how pointers work.
Comments
Post a Comment