Definition: In simple words pointer is a variable that stores address (memory location) of another variable's. This means that a pointer holds the memory address of another variable.
There are two main operator's used in pointer:
1. Reference operator &
2. Dereference operator *
1. Reference operator (&):
The & is a unary operator that returns the memory address of its operand.
Syntax of reference operator &:
&variable-name;
- For Example:
If you have any variable var in your program, &var will give you it's memory address.
We have already used address(&) numerous times while using the scanf() function:
scanf("%d",&var);
Examples program to print address of variable:
Here, %p is format specifier which is used to print the address of a pointer.
#include<stdio.h>
int main()
{
// declare variable
int var;
printf("Address is: %p\n", &var);
return 0;
}
Above program shows the following output:
2. Dereference operator (*):
The * is a unary operator that returns the value of the variable at the address specified by its operand.
Syntax of dereference operator (*):
*memory-address or pointer-variable;
Example program of dereference operator (*):
#include<stdio.h>
int main()
{
int var=10;
/* It prints value pointed by (&var)*/
printf("Pointed value is: %d",*(&var));
return 0;
}
Above program shows the following output:
Declaration syntax of pointer in C:
Above program shows the following output:
Advantages of using pointer:
1. Pointer provide direct memory access.
2. It reduces the execution time of the program.
3. It reduces the storage space and complexity of the program.
4. Pointer provides an alternate way to access array elements.
5. Using pointers, arrays and structures can be handled in more efficient way.
Disadvantages of using pointer:
1. In pointer if sufficient memory is not available for the storage of pointers during runtime, the program may crash.
2. Pointers are slower than normal variables.
3. Uninitialized pointers might cause segmentation fault.
Learn Pointers with more Examples....
No comments:
Post a Comment
If you have any doubts, please discuss here...👇