Table of Contents
Pointers in C Programming Language
In this tutorial, we will discuss the concept of Pointers in C Programming Language
Knowledge area
- What is the pointer in C
- Declaration & initiation of pointer
- find variable address & pointer
- Advantages and disadvantages of pointer
pointer
- The pointer is one of the features of C language like C++
- The pointer is a very fundamental and important concept program in C Language.
- The pointer is one of the variables is basically same as other variables in C. Which can store a piece of data
- The normal variable which stores a value such as an int, a double. a char.
- A pointer store a memory address is similar to an array but array is statically pointer is dynamical. dynamically means we can increase or decreased memory allocation to my need.
- So the pointer is a special type of variable that contains just the address of another variable. it is called pointer
what is a memory unit
As we know, a memory unit is a collection of storage cells which can store information
Each memory cell is given an identification by address(some times may be number), it’s called address
if memory can store n information it addresses ranges from 0 to n-1.
Declaring and initialising pointers
All pointers variable must be declared before it is used in the program like other variables. The general form is,
Syntex
var – it is a variable on your program in C
&var – it is an address of your variable in the memory location
*var – it is a pointer of your memory location
data type* var; // Dicare a pointer variable called var as the pointer of type
data type *var;
data type * var;
– data type means such as int, float double etc
* (sign) – to identify the variable as a pointer
*ptr – this is a pointer variable
var- this is a normal variable
Example :
int *a; // Decare a pointer variable called as pointing to an int
// it contain an address holds an int value
or
char* a; // Dicare a pointer variable called an as pointing to a char
// it contains an address holds a char value
or
double * a; // Dicare a pointer variable called a as pointer a double
// it contains an address holds a double value
Program 1
How to find the variable address at the memory location
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=0; //assign int type a, initialized value 0
int b=25; //assign int type b,and initialized value 25
float c=3.45; //assign float type c and initialized 3.45
printf(“value of a:%dn”,a); //disply value of a
printf(“value of b:%dn”,b); //disply value of b
printf(“value of c:%fn”,c); //disply value of c
printf(“……………n”);
printf(“address of a:%un”,&a); //disply address of a in memory location
printf(“address of b:%un”,&b); //disply address of b in memory location
printf(“address of c:%un”,&c); //disply address of b in memory location
return 0;
}
- & to be manipulated in a reference variable
Program 2
how to Find pointer value is address of variable
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a; //assign int type a,but not initialized
int b=25; //assign int type b,and initialized value 25
float c=3.45; //assign float type c and initialized 3.45
printf(“address of a:%un”,&a); //disply address of a in memory location , //&a address of a
printf(“address of a:%un”,&b); //disply address of b in memory location
printf(“address of a:%un”,&c); //disply address of c in memory location
int *p1; //assign a pointer of int type p1;
p1=&a; //assign address of a to pointer p1
int *p2=&b; //assign a pointer of int p2 and
//assign address of b to pointer p2 same line
float *p3=&c; //assign a pointer of int p3 and
//assign address of c to pointer p3
printf(“n”);
printf(“value of p1 is:%un”,p1); //disply value of p1 which is address of a
printf(“value of p2 is:%un”,p2); //disply value of p2 which is address of b
printf(“value of p3 is:%un”,p3); //disply value 0f p3 which is address of c
return 0;
}
Advantages of pointers
pointer increased the execution speed of the program
Pointers reduced the length and complexity of a program
Pointer enables a variable that is defined outside the function
a pointer used to pass information back and forth between a function and it’s the reference point
Disadvantages of pointers in C language
Uninitialized pointers might cause a segmentation fault
pointers are slower than other variables
if sufficient memory is not available during runtime for the storage of pointers the program may crash
Related Links
Pointers in C++ Programming language
Pointer to pointer in C Programming Language
Pointer to pointer in C++ Programming Language
Pointers with one d array in C language
Structure with pointers in C Language
Structure with pointers in C++ Language
Pointers and one d array in C++