Pointer In C
The
pointer in C language is a variable which stores the address of another
variable. This variable can be of type int, char, array, function, or any other
pointer. The size of the pointer depends on the architecture. However, in
32-bit architecture the size of a pointer is 2 byte.
Fig: Pointer in C
Example
int n = 10;
int* p = &n;
Advantage of pointer
1)
Pointer reduces the code and improves
the performance, it is used to retrieving strings, trees, etc. and
used with arrays, structures, and functions.
2)
We can return multiple values from a function using
the pointer.
3)
It makes you able to access any memory location in
the computer's memory.
1)
Pointer reduces the code and improves
the performance, it is used to retrieving strings, trees, etc. and
used with arrays, structures, and functions.
2)
We can return multiple values from a function using
the pointer.
3)
It makes you able to access any memory location in
the computer's memory.
Usage of pointer
There
are many applications of pointers in c language.
1) Dynamic memory allocation
In
c language, we can dynamically allocate memory using malloc() and calloc()
functions where the pointer is used.
2) Arrays, Functions, and Structures
Pointers
in c language are widely used in arrays, functions, and structures. It reduces
the code and improves the performance.
Declaring a pointer
The
pointer in c language can be declared using * (asterisk symbol). It is also
known as indirection pointer used to dereference a pointer.
int *a;//pointer to int
char *c;//pointer to char
Program
#include<stdio.h>
int main(){
int number=50;
int *p;
p=&number;//stores the address of number variable
printf("Address of p variable is %x \n",p);
printf("Value of p variable is %d \n",*p);
return 0;
}
Output
Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 50
There
are many applications of pointers in c language.
1) Dynamic memory allocation
In
c language, we can dynamically allocate memory using malloc() and calloc()
functions where the pointer is used.
2) Arrays, Functions, and Structures
Pointers
in c language are widely used in arrays, functions, and structures. It reduces
the code and improves the performance.
Declaring a pointer
The
pointer in c language can be declared using * (asterisk symbol). It is also
known as indirection pointer used to dereference a pointer.
int *a;//pointer to int
char *c;//pointer to char
Program
#include<stdio.h>
int main(){
int number=50;
int *p;
p=&number;//stores the address of number variable
printf("Address of p variable is %x \n",p);
printf("Value of p variable is %d \n",*p);
return 0;
}
Output
Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 50
Pointer to array
int arr[10];
int *p[10]=&arr;
int arr[10];
int *p[10]=&arr;
Pointer to a function
void show (int);
void(*p)(int) = &display;
void show (int);
void(*p)(int) = &display;
Pointer to structure
struct st {
int i;
float f;
}ref;
struct st *p = &ref;
struct st {
int i;
float f;
}ref;
struct st *p = &ref;
NULL Pointer
A
pointer that is not assigned any value but NULL is known as the NULL pointer.
If you don't have any address to be specified in the pointer at the time of
declaration, you can assign NULL value. It will provide a better approach.
Example:
int *p=NULL;
A
pointer that is not assigned any value but NULL is known as the NULL pointer.
If you don't have any address to be specified in the pointer at the time of
declaration, you can assign NULL value. It will provide a better approach.
Example:
int *p=NULL;
No comments:
Post a Comment