Pointers in C Programming
80In this tutorial I am going to discuss what pointer is and how to use them in our C program. Many C programming learner thinks that pointer is one of the difficult topic in C language but it’s not completely true. It is difficult to understand in comparison of other secondary (array, structure etc.) data types available in C. Pointer in C seems to be difficult because it works directly with computer memory (RAM).
When we declare any variable in C, for example integer variable, it occupies a memory according to its size (here 2 bytes). As it occupies a memory so it’s natural to have an address for this location so that it can be referenced later by CPU for manipulation. But before we begin with Pointer we must have some idea on the operators we are going to use in Pointer programming.
The ‘*’ And ‘&’ Operators
Take a look on the following declaration,
               int x=10;
When we make such declaration how C compiler treat this statement:
- Reserve memory space for this integer value.
- Name this memory location as x.
- Store the value 10 at this location.
This can be illustrated by a simple diagram:
So when we declared an integer variable x and assigned value 10 then compiler occupied a 2 byte memory space at memory address 65325 and stored value 10 at that location. Compiler named this address x so that we can use x instead of 65325 in our program. But we can use address or variable in our program and both will point to the same value (example given below).
The memory address of a variable could differ from PC to PC and entirely depends on available free space in memory at time of program execution. As this differs from PC to PC thus we cannot rely on that address (numeric value representing variable address) and we cannot use this address in our program. But have you noticed one thing that address is an integer.
We can print address of any variable or function, following program shows how we can do that:
Address of variable example.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=9;
clrscr();
printf("Value of i : %d\n",i);
printf("Address of i : %u",&i);
getch();
}This is a very simple c program which prints value and address of an integer. But did you notice line no. 10 in above program? This line output the address of i variable and to get address of i variable we have used ampersand (&) operator. This operator is known as "Address of" operator and we already used this operator many times in our program, just recall scanf statement which is used to accept input from computer keyboard. So when we use ampersand operator (&i) before any variable then we are instructing c compiler to return its address instead of value.
Another operator we going to deal with in this entire pointer tutorial is "*" called "Value at address" operator. It is the same operator which we use for multiplication of numbers. As the name suggest, "value at address" operator returns value stored at particular address. The "value at address" operator also called indirection operator. Following example extends above C program and puts "value at address" operator in action.
Value at address (*) example
#include<stdio.h>
#include<conio.h>
void main()
{
int i=9;
clrscr();
printf("Value of i : %d\n",i);
printf("Address of i : %u\n",&i);
printf("Value at address of i : %d",*(&i));
getch();
}
Now in this program notice line no. 11 and use of ‘&’ and ‘*’ operators also see the output of program which prints value of variable i and *(&i) same. This is how these two operator works.
You must have clear idea of ‘&’ and ‘*’ operator usage before you jump into pointer because pointer deals with these to operator. If you find any difficulty then post your doubts and I will try to clear your doubts.
|
C Programming Language (2nd Edition)
Price: $38.80
List Price: $60.33 |
|
Programming in C (3rd Edition)
Price: $29.13
List Price: $49.99 |
|
C Programming: A Modern Approach, 2nd Edition
Price: $58.69
|
|
C Primer Plus (5th Edition)
Price: $21.99
List Price: $54.99 |
|
Absolute Beginner's Guide to C (2nd Edition)
Price: $17.63
List Price: $34.99 |
|
Microsoft Visual Studio 2008 Professional
Price: $579.00
List Price: $799.00 |
|
Learn Objective–C on the Mac (Learn Series)
Price: $18.98
List Price: $39.99 |
|
Ectaco C-Pen 20 Handheld Scanner
Price: $100.95
List Price: $199.95 |
|
Kindle Wireless Reading Device (6" Display, Global Wireless, Latest Generation)
Price: $259.00
List Price: $259.00 |
|
Sony Bravia L-Series KDL-32L5000 32-Inch 720p LCD HDTV, Black
Price: Too low to display
List Price: $499.99 |
|
HP Pavilion DV6-1354US 15.6-Inch Black Laptop - Up to 4 Hours of Battery Life (Windows 7 Home Premium)
Price: $699.99
List Price: $799.99 |
|
Sony VAIO VGN-NW240F/P 15.5-Inch Pink Laptop (Windows 7 Home Premium)
Price: $729.00
List Price: $729.99 |
|
Apple Magic Mouse
Price: $69.00
List Price: $69.99 |
|
Toshiba 500 GB USB 2.0 Portable Hard Dive HDDR500E04XL (Liquid Blue)
Price: $89.88
List Price: $129.99 |
|
Apple iPod touch 32 GB (3rd Generation) NEWEST MODEL
Price: Too low to display
List Price: $295.00 |
|
Apple iPod nano 16 GB Black (5th Generation) NEWEST MODEL
Price: Too low to display
List Price: $175.00 |
What is a Pointer?
A pointer is a secondary data type (also known as derived data type) in C. It is built from one of the primary data types available in C language. Basically pointer contains memory address of other variable or function as their value. As pointer deals with memory address, it can be used to access and manipulate data stored in memory.
Benefits of using Pointer in C Program
Pointer is one of the most exciting features of C language and it has added power and flexibility to the language. Pointer is in C language because it offers following benefits to the programmers:
- Pointers can handle arrays and data table efficiently.
- Pointers support dynamic memory management.
- Pointer helps to return multiple values from a function through function argument.
- Pointer increases program execution speed.
- Pointer is an efficient tool for manipulating structures, linked lists, queues stacks etc.
We can achieve these and much more benefits from pointer only if we can use it properly.
Pointer with an example
Since this tutorial is already become lengthy so I am going to give a small example on how we can use pointer in our C program. Of course we will discuss remaining part in my next pointer tutorial.
Syntax: Â Â Â Â Â Â Â Â Â Â datatype *pointer_name;
Example : Â Â Â Â Â Â int *iPtr;
                       float *fPtr;
Pointer Example
#include<stdio.h>
#include<conio.h>
void main()
{
int i=9, *ptr;
ptr=&i;
clrscr();
printf("Value of i : %d\n",i);
printf("Address of i : %u\n",&i);
printf("Value of ptr : %u\n",ptr);
printf("Address of ptr : %u\n",&ptr);
printf("Ptr pointing value : %d", *ptr);
getch();
}In the above example in line no. 6 we have declared an integer i and assigned 9 to it. Along with variable i, we have also declared an integer pointer. To declare a pointer of any data type we just need to put an * (asterisk) before variable name or identifier. In our example ptr is an integer variable and is capable of holding address of any integer variable.
Remember one thing integer pointer can hold only integer variable address, you cannot use integer pointer to hold address of float or char variable. To hold char or float variable address in a pointer you need to declare char or float pointer respectively.
In line no. 7 we assigned integer variable i’s address to ptr, now ptr is pointing to i. Line no 10 prints value of i and line no. 11 prints address of i. Next line prints value stored in ptr pointer variable, line no. 13 prints address of ptr pointer. As pointer is different entity so it also requires space in memory. Memory occupied by a pointer depends on its data type. Integer pointer will occupy 2 bytes whereas character pointer will occupy 1 byte. Finally line no. 14 prints the value of address stored in ptr pointer i.e. value of i. And to print value of stored address in a pointer we need write *pointer variable name (in our example *ptr). A memory map will help you to understand this.
If you find any difficulty or error in this article then please let me know through comment box.
Thank you for visiting my free C pointer tutorial.Â
C Programming Leaner's Poll
Is this tutorial helpful to understand basic C Pointer concept?
See results without votingC Programming Tutorial Links
- A Brief History of the C Language
Before we start any complex program in C, we must understand what really C is, how it came into existence and how it differs from other languages of that time. In this tutorial I will try to talk about these... - Data Types in C Language
A programming language is proposed to help programmer to process certain kinds of data and to provide useful output. The task of data processing is accomplished by executing series of commands called... - Array in C programming Programmer's view
An array in C language is a collection of similar data-type, means an array can hold value of a particular data type for which it has been declared. Arrays can be created from any of the C data-types int,... - How to work with Two Dimensional Arrays in C
We know how to work with an array (1D array) having one dimension. In C language it is possible to have more than one dimension in an array. In this tutorial we are going to learn how we can use two... - How to work with Multidimensional Array in C Programming
C allows array of two or more dimensions and maximum numbers of dimension a C program can have is depend on the compiler we are using. Generally, an array having one dimension is called 1D array, array... - Types of Function in C Programming Languages:
In my previous c programming tutorial I tried to explain what the function, its advantages is and how to declare a C function. And I told you that there are five types of functions and they are: ... - What is Function in C Language?
A function in C language is a block of code that performs a specific task. It has a name and it is reusable i.e. it can be executed from as many different parts in a C Program as required. It also ...
PrintShare it! — Rate it: up down flag this hub










puspak says:
7 days ago
it is awesome