Table of Contents
In this tutorial, we will discuss the concept of Pointers in C Programming Language
Knowledge area
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.
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;
}
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;
}
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++
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.