Home Theory Playing with Pointers

Playing with Pointers

by nikoo28
0 comment

Question: What is a Pointer? What are its limitations? What are its benefits? How do we use it? What all operation we can perform using it?

In this article we are going to discover answers to all these questions.

Pointer is a variable that contain the address of another variable and that variable can be a structure, array or basic data type. The basic syntax to define the pointer is as follows:
Datatype * identifier;
Datatype is the data type of the variable whose address we are going to store in the pointer.
* is known as Indirection or De-referencing operator to reference the variable’s value whose address is stored with the pointer.
identifier is the Identifier that we use for the pointer variable.
Pointers allow us to manipulate the memory and change the actual values stored in the memory locations and they form very strong feature of C language. Whenever we define a Pointer to a datatype we say that the Pointer points to it. E.g. – int * example – pointer to integer

Pointer and Array have a very strong relationship in C. An array sub-scripting can be done by using pointer also and the pointer version will be faster in general. So the i-th element in an array int arr[n] of size n can be accessed by *(arr + i). We are able to do so because array name is its base address in C. But there is one important difference between an array name and a pointer. A pointer is a variable, so

ptr=arr

and

ptr++

are legal, but an array name is not a variable i.e. constructions like

arr=ptr

and

arr++

are illegal, where definition of arr and ptr are: int arr[n] and int *ptr respectively.

Operations on Pointers

Let’s now see the valid operations with Pointers. The valid pointer operations are assignment of pointers of the same type, adding or subtracting a pointer and an integer, subtracting or comparing two pointers to members of the same array, and assigning or comparing to zero. All other pointer arithmetic is illegal. It is not legal to add two pointers, or to multiply or divide or shift or mask them, or add float or double to them, or even for valid *, to assign a pointer of one type of pointer of another type without a cast. However, if the two pointers points to members of the same array, then binary relations like == , != , < , >= works properly i.e. relational operators.
C also defines a generic pointer, the type void * (pointer to void). It can be cast into any type of pointer and it is default return type of library function malloc(). void * cannot de-reference itself so it always needs to be converted into another type by an explicit cast. Let’s see all this aspects of pointers by the following code:

#include<stdio.h>

int main(void)
{
	// Declaring a void pointer
	void *vptr;
	
	// Creating some pointer variables
	int arr[5]={34,5,17,39,1};
	int *ptr1,*ptr,num,i;
	
	vptr=arr;
	
	ptr=(int *)vptr;	//  casting void *
	
	ptr1=arr+3;		   // pointer and integer addition
	
	if(ptr1 > ptr)		     //comparison of pointers
		num=ptr1-ptr;		// subtraction of pointers

	printf("% d,% d,% d\n",*ptr,*ptr1,num);
	
	ptr = ptr1-2;		//pointer and integer subtraction
	ptr1++;			    //pointer increment
	
	printf("% d,% d\n",*ptr,*ptr1);
	
	return 0;
}

Output:
34 39 3
5 1

Following would be invalid operation in the above code :

// invalid operations
ptr1  = arr + ptr ;
ptr1 +=ptr;
ptr  *=3;
ptr1 /=ptr;
char *cptr  =ptr;
ptr =*(arr +4.2);
ptr <<2;

You may replace these lines in above code(obviously omitting the comment characters) to check that these are invalid operations. Here is the link to the running sample of the code-

http://ideone.com/xQfR5T

This is a basic and introductory article on pointers meant mainly for the readers who are new or ‘not an expert’ with pointers to get an easy understanding of pointers. Pointers extend to multidimensional pointers, function pointers etc. which we will cover in further posts so keep tuned in ;-)

0 comment

You may also like

Enclose codes in [code lang="JAVA"] [/code] tags

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More