C Pointers

Pointer is a derived data type in C which is constructed from fundamental data type of C language.

Advantages:
By using pointers we can access dat which is available outside of the function.
By using pointers we can handle run time memory management i.e. dynamic memory allocation.
By using pointers we can handle the data structure more efficiently.
By using pointers we can increase the execution speed of the program.
When we are working with the pointer we need to use following operators:
&(Address of operator)
*(Indirection operator/dereference operator/Object of location/Value of add)
Address operator(&) always returns base address of a variable. Indirect operator(*) Always returns value of a address.

Syntax:

datatype *Pointer;

Example:

void main()
{
int i;
int *ptr;
ptr=&i;
}

Where i is a variable of type integer, it is a value type variable which holds always an integer value.
ptr is a variable of type int *, it is a address type variable which always holds an integer address.

void main()
{
int a,b;
int *ptr;
ptr=&a;
a=10;
b=20;
printf(“”\n %d % d %d”,a,b,*ptr);
*ptr=30;
ptr=$b;
*ptr=40;
printf(“\n %d%d%d”,a,b,*ptr);
}

Output:

10 20 10
30 40 40

Depending on the memory models pointers are classified into three types.
C Near Pointer
C Far Pointer
C Huge Pointer

Powered by k2schools