Skip to main content

pointer in c with example

Hello, hope you are well. So now we will talk about pointers in C programming language. Today we talk about various sub topics of pointers are:- What is a pointer, Printing address present in pointer,  Accessing a memory block with help of pointer, pointer to a pointer in c and Pointer to an Array. Read full article to better understanding, Lets start...

pointer in c programming
pointer in c

Introduction of pointer in c:-

To store data, we can create a variable or we can create an array. A variable is of scalar type while an array is of aggregate type. Whenever an array or a variable is created, space is allocated in the memory(RAM).

For eg:-

        int   a[5];      //array
int   x; //variable(scalar)

Introduction of pointer in c

Each and every memory block which is created during the execution of a program is assigned a unique address automatically. The address in the hexadecimal format.

0 - 9
A - 10
B - 11
C - 12
D - 13
E - 14
F - 15

We can excess a memory block using its address. This is done using the concept of pointer.

Also Read: Control Statements In C - language

Pointer allow as to work on memory block with help of the its address. Pointer is used to perform dynamic memory allocation. In dynamic allocation, the size is specified at run time. The memory is allocated at run time and as application executes(run), The size of memory can grow or 
shrink(decrease).

What is a pointer in c?

A pointer is a special variable which stores address of some another variables. To create a pointer variable, We use the following syntax:-

 

datatype * pointer var name;
The datatype specified at the time of creating the pointer is the data type of the variable whose address can be saved in the pointer.

For ex:-

long * ptr;
/*(here ptr is a pointer which can store address of any variable of long type only)*/
To assign address of a variable to a pointer variable, we use &(address of operator).
Syntax:-

pointer name=&variable name;
For ex:

int  d;
int  *p;
p=&d;

What is a pointer in c?

C Printing address present in pointer

a pointer is a special variable which stores address of some another variable. Once a pointer has been assigned address of some variable, we can print/display the address int he pointer by using type specifier %p in printf().

Syntax:-

printf("%p",pointer var name);
sample code:-


//program using Printing address present in pointer

#include<conio.h>

#include<stdio.h>
void main() { lang *ptr; /*here ptr is a pointer which can stored address of any variable of type long.*/ long d; clrscr(); ptr=&d; /*the address of a variable 'd' have been stored in the pointer named ptr*/ printf("the address of d is %p",ptr); /*here pointer named ptr is being printed. Whenevr a pointer is printed, the address in that pointer gets printed*/ getch(); }

C Accessing a memory block with help of pointer 

A pointer stores address of a memory block. Using a pointer, we can access a memory block. To do so, we use *(dereference operator).

Whenever dereference operator is used with the pointer name, then the pointer can be used to access the memory block. Whose address is present in the pointer.

syntax:-

*pointer name;
whenever * is used with the pointer name, it denotes value at the complete meaning of  * followed by pointer name his value at that address.

For Eg:

float *x;
float a;
a=90.8;
x=&a;
 Referring to the address present in the pointer is pointer name. Value present in the variable where address is with pointer is *pointer name.
Sample code:-


//program Accessing a memory block with help of pointer 

#include<conio.h>

#include<stdio.h>
void main() { long *a; long d=600000; clrscr(); a=&d; printf("the address of d is %p",a); printf("\n the value at d is%ld",*a); getch(); }

pointer to a pointer in c

In c programming language, A special variable called pointer variable stores the address of a variable. Each and every variable is assigned a unique address by the operating system. Pointer store this address and allows manipulation of the variable through the address stored in it.

A pointer variable is also assigned on address. To store the address of a pointer variable, we create a special type of reference variable called pointer to a pointer.

Creating pointer to a pointer

datatype **pointer name;
Here pointer which will store address of some another pointer which is storing address of a variable of type int. To store address of a pointer in another pointer, we use &(address of operator).
Syntax:-

pointer to a pointer name = &pointer name;
sample code:-


//program Creating pointer to a pointer 

#include<conio.h>

#include<stdio.h>
void main() { int x; //variable int *a; //pointer int **b; //pointer to a pointer a=&x; /*address of x assigned to a pointer a*/ b=&a; /*address of pointer a assigned to pointer to pointer b*/ printf("the address of x is%p",a); printf("\nthe address of a is%p",b); getch(); }

C Pointer to an Array

A pointer is a reference variable which stores the memory address of a variable or an array or a structure. Each and every variable which is created in an application is assigned unique address by the system. This address can be used to access the memory block. We can create a special pointer which points to an array. Such a pointer is called pointer to an array.

C Pointer to an Array
C Pointer to an Array

To create pointer to an array, we use same syntax as used when creating a pointer to a scalar variable.

Syntax:-

   datatype  *pointer name;
For Ex:

float   *b;     /*here b is a pointer to an array of the float */
Once pointer to an array has been created, we can use the following techniques to store address of an array in that pointer.

1) pointer name=array name;

address of array will be stored in the pointer.

For Ex:

int  *p;
int  a[5];
p=a; //address of a assigned to p

2)pointer=&array name[0];

For Ex:

int  *ptr;
int  c[10];
ptr=&c[0];
Once address of an array has been assigned to a pointer, then we can use the pointer to access each and every element of the array. The pointer name can be used just like the array name.

Sample code:-


//program of pointer to an array 

#include<conio.h>

#include<stdio.h>
void main() { int i; int *p; int a[10]; clrscr(); printf("enter 10 values\n"); for(i=0;i<=9;i=i+1) { scanf("%d",&a[i]); } p=a; /*address of a assigned to pointer p */ for(i=0;i<=9;i=i+1) { printf("%d",p[i]); } getch(); }

Comments

Popular posts from this blog

Control Statements in c language

So, Today we are going on new topic name control statements in C language. In which we read Looping(For, While and Do While), Jumping(Break, goto and Continue) and Decision Control statements(Switch, If, If Else and  If else if ladder)  in C. Before you read this article May you also like our previous posts :-    Structure Of C Language Program   Variables & Datatypes In C Language    Format Specifier In C Programming  .   It may help you to understanding it. A Statement is the smallest unit that is a complete instruction in itself statements contains expression and usually end with a semicolon. Statements are of two Types:- Types of Statements in C  1. Sequential Statement   2. Control Statement Sequential Statements are statements which are executed by one in sequential manner. Control Statements are the statements which alter the flow of execution and provide better control to the programme. On the flow of execution. They are useful to write better and complex program. Control S

Beginner our view on C programming Language

  Beginner our view on C programming Language   Beginner ourview on C programming Language History about C programming Language. The founder or we can say developer of C programming language was Dennis Ritchie. He was found in  Bell labs USA. The Birth of C language is originally on 1972. After the time C is widely popular all around the world. C is also used in to develop many OS system some are Linux and Windows. Now days C is still widely used or popular language in Programming world. Here, we have a question is What is a Program ?  A Program is a set of instruction which on   execution by a machine produces a specific result. A machine needs set of instructions to perform an operation. Those set of instruction are contained in a program. What is program? What is Programming languages ? What Is a Programming Languages? Meaning- A programming language allows a programmer to develop instructions which can be executed by a machine. A programming language acts as an intermediate bet

What is Blogger? How we create a account on it.

What is Blogger ? How we create a account on it. Welcome to blog. Here I am talking about blog on blogger.com. Hope you guys like it, Thanks. Here, I will talk about Ø What is blog. Ø About blogger. Ø How to create an account on blogger. Ø Can we earn money to blog. What is Blog Blog is a kind of platform, where you can write your thinking about that particular topic. Also you can say blog is article which we write through blogger.com and WordPress, etc. After writing the blog we go to publish in internet. If we write on a unique content hope traffic will came on our blog. You should write a blog on different topics like - Games, Technical, programming, Knowledge, education, and many more you like. What is blogger.com     Blogger is website that manages by Google. Google bought blogger from Pyra labs in 2003.Blogger is free for all, it give space to our passion in his platform. Around the world millions of peoples are used blogger daily and post something every