Reading:  

Getting started with C Language


Pointers in C

Pointers

Pointers are one of the powerful tools in C language. Pointers are the variables which hold memory address of another variable or in other words Pointers are the variables that store the location of a data item , such as variable or array element . We know the computer use a memory to store the data items which are stored in memory cells. The computer’s memory is a sequential collection of storage cells, each cell commonly known as bytes. Each byte has an address associated with it, this address is normally a number.  The size of data or the number of memory cells required to store a data item depends on the type of the data item we are used. For example, a character will be stored in 1 byte of memory where as an integer requires two contiguous bytes, but  a floating-point number usually requires four contiguous bytes. But in the case of arrays we want to calculate the size by size of data item * number of allocated size. If we declare an integer array of size 10 we require 2(size of an integer) * 10 (the array size) bytes which is 10 * 2 =20 bytes. Thus it means how the data is stored in memory. Pointers deals with this allocation of the memory.

Why we use pointers  

  • Pointers enable us to access a variable that defined outside the function.
  • Efficient in handling the data tables.
  • Reduce the length and complexity of a program.
  • Save the data storage space in memory.
  • Increase the execution speed.

Whenever a variable is declared the computer allocates that variable somewhere in the memory. Suppose p is a variable that store the value 10. The compiler will assign memory to this data item.

The data item can then be accessed if we know the address of the first memory cell. The address of p’s memory location can be determined by the expression &p, where ‘&’ is the operator, called the address operator, that finds the address of its operand here it is ‘p’. If we want to assign the address of ‘p’ to another variable ptr then that variable  ‘ptr’ will be a pointer. That is

 ptr = &p;

This new variable ptr is called pointer to p. since it points to the location where p is stored in address , not its value here it is (1008).  Thus ptr is referred to as a pointer variable. The relationship between ptr and p is illustrated in the following figure.

 

Where address of p is the value of ptr. Where ptr itself has an address (5000) and it stores the value 1008 which is the address of p.

The value holds in p (the data item stored in p’s memory cells) can be accessed by the expression *ptr where '*'  is a unary operator called the indirection operator, that operates on a pointer variable. Therefore, *ptr and p both represent the same data item. That is *ptr gives the value stored in  the memory location (which the ptr holds) and p gives the value which it stores. Here in this case it is 10.

 

Pointer declaration :

Pointers are the data types like a normal variable, but holds address of another variable which must be declared before they may be used in the program.

Syntax :            

Data-type *pointer-name;

When a pointer variable is declared, the variable name must be preceded by an asterisk (*). This identifies the fact that the variable is a pointer. The data type that appears in the declaration refers to the object of the pointer. i.e. the data item that is stored in the address represented by the pointer, rather than the pointer itself.

Example :

int a, *b , c;

a = 100;

b = &a;

c = *b;

thus 100 is stored in c.

Operation on pointers

Arithmetic operations can be performed on pointer variables.

Unary operations :

++ (increment) and – ( decrement )

Binary operations :

+ (addition) and – (subtraction)

If ptr1 and ptr2 are properly declared and initialized pointers then the following statements are valid

A = *ptr1 * *ptr2 ;

Which means (*ptr1) * (*ptr2)

sum = sum + *ptr1 ;

a = *ptr1 / 2;

ptr1 = ptr2 + 2;

ptr2 =  ptr2 + 1;

Pointers and arrays

When an array is declared the compiler allocates a base address and  sufficient amount of storage to contain all the elements of the array in contiguous memory locations. The base address is the location where the first element is stored. 

Example :

int x[5]= { 1 , 2 , 3 , 4 , 5} ;
ptr = x;
ptr = &x[0];

here ptr is a pointer.

Description

This tutorial introduces you to C language. Many tutorials has been written. We are writing this tutorial to be brief and to the point, so that you concentrate on what matter, rather than finding yourself in a mess. We want you to get a start and build form knowledge you will gain from this tutorial



Environment

We will cover environment needs as we progress through this tutorial. At the very least you will need a PC with windows or Linus operating system.

Audience

Programmers new to C Language

Learning Objectives

Learn basics of C language and build from that knowledge.

Author: Subject Coach
Added on: 3rd Jan 2015

You must be logged in as Student to ask a Question.

None just yet!