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. The...

Array in C Language Btech Bca Notes

 Array in C Language Btech Bca Notes An array is a data form that can hold several values all of the same type. An array is a collection of variable of the same type that are referral to by a common name. Also Read:   Beginner our view on C programming Language  . Array are vital for most programming language. They are collection of variables, which we call elements. Each of the element in an array is stored sequentially in the computeric memory.  This making it easy to  m anipulate them and navigate among them. Since all the elements in an array are of same type, array allows us to represent a group of similar elements as an ordered  sequence  and work on them as a whole (grouping together related data). Array organize data in such a way that it can be easily sorted. To create an array, we use a declaration statement. An array declaration should indicate three things: 1. The type of value to be stored in each element. 2. The name of the array. 3. The ...

Format Specifier In C Programming Btech BCA Notes

   Format Specifier In C Programming Btech BCA Notes Welcome, Hope you are doing well. Today we are talking about Foramat specifier in C language. We can also say Type Specifier In C.  So, Today we are read formate specifier as well as printf and scanf Formats in different situations and different conditions. Before you read this article May you also like our previous posts :-    Beginner our view on C programming Language     Structure Of C Language Program   Variables & Datatypes In C Language  . It may help you to boost Your Information. In C Programming language, Types specifier is a special piece of code which is used during the input and output. Type specifier inform the compiler about the type of data which will be entered or displayed during an input output operation. Input mans reading data from keyword while output means displaying data either on screen or a printer. In c – Language every datatype has its own type specifier...