Pointer in C programming with examples

C Pointer With Examples

A pointer in c is used to store the address of any variable. it directly points to the memory location.
By using a pointer, we can work faster on memory than a variable.

int a=5;
(i) it reserves space of integer to hold memory.
(ii) The Memory location is a.
(iii) the value at memory location is 5.

How to declare a pointer in c?

int *p1  /*Pointer to an integer variable*/
double *p2  /*Pointer to a variable of data type double*/
char *p3   /*Pointer to a character variable*/
float *p4  /*Pointer to a float variable*/ 

Explanation of C Pointer with examples

Example 1:-

1
2
3
4
5
6
7
8
9
10
#include<stdio.h>
#include<conio.h>

int main()
{
int a=5;
printf("Address of a=%p\n",&a);
printf("The value of a is=%d\n",a);
getch();
}

Output:-

Example 2:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
int main()
{
   /* Integer type Pointer, this can hold the
    * address of a integer type variable.
    */

   int *a;

   int var = 20;

   /* Assigning the address of variable var to the pointer
    * a. The a can hold the address of var because var is
    * an integer type variable.
    */

   a= &var;

   printf("Value of variable var is: %d", var);
   printf("\nValue of variable var is: %d", *a);
   printf("\nAddress of variable var is: %p", &var);
   printf("\nAddress of variable var is: %p", a);
   printf("\nAddress of pointer p is: %p", &a);
   return 0;
}

Output:-

Some More Stuff on Pointer in C

1) Pointer to Pointer– A reminder can point to one more tip (which indicates it can save the address of an additional guideline), such reminders are called double tip OR pointer to a pointer.

2) Passing a pointer to function– Reminders can also be passed as an argument to a feature, utilizing this function a feature can be called by referral as well as a variety that can be passed to a function while calling.

3) Function pointers– A featured reminder is just like an additional reminder, it is utilized for storing the address of a function. Function tips can also be utilized for calling a feature in the C program.

Leave a Reply