BY RSS |
What is pointer in C?
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.
Why we use pointers?
pointers are used to create dynamic data structures -- data structures built up from blocks of memory allocated from the heap at run-time.pointers are also used to handle variable parameters passed to functions. Pointers provides an alternative way to access information stored in array
How to Use Pointers?
There are a few important operations, which we will do with the help of pointers very frequently. (a) We define a pointer variable, (b) assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand.
NULL Pointers
It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.
Sr.No.
|
Concept & Description
|
1
|
There are four arithmetic operators that can be used in pointers: ++, --, +, -
|
2
|
You can define arrays to hold a number of pointers.
|
3
|
C allows you to have pointer on a pointer and so on.
|
4
|
Passing an argument by reference or by address enable the passed argument to be changed in the calling function by the called function.
|
5
|
C allows a function to return a pointer to the local variable, static variable, and dynamically allocated memory as well.
|
Example of pointers:-
#include<stdio.h>
int main()
{
int i,a[4]={10,20,30,40};
int *p;
p=a;
for(i=0;i<4;i++){
printf("\n%d" ,*p);
printf("\n %d",p);
p++;
}
}
Null pointer:-
#include <stdio.h>
int main () {
int *ptr = NULL;
printf("The value of ptr is : %x\n", ptr );
return 0;
}
Pointer In 2d array:-
#include<stdio.h>
int main()
{
int arr[2][3] = {
{ 10, 11, 12 },
{ 20, 21, 22 }
};
int i, j;
for (i = 0; i < 2; i++)
{
printf("Address of %dth array = %p %p\n",
i, arr[i], *(arr + i));
for (j = 0; j < 3; j++)
printf("%d %d ", arr[i][j], *(*(arr + i) + j));
printf("\n");
}
return 0;
}
Note::-
If there any pointer is placed in the front of function eg:- int *pte() then it is called as self refrrential pointer
Thank You..
ReplyDelete