Pointers in C with examples | Pointers | C Tutorials

C Pointers

In general all beginners have a dislike for pointer in C. If the users have experience with any programming language, they must have been familier with the concept of branching, looping, use of functions to divide the program into small managable modules or even use of arrays.

Pointer is a unique feature of C language. The beginners of C language develop a dislike for pointers because it is a completely new concept with which they are not familier. Is there any situation where one can not write a program without the knowledge of pointers?

Without pointers, C language will have some serious limitations and it will loose much of its potential. For example, a C function can not return more than one value. One can not write a C funtion to evaluate and return both the roots of quadratic equation.

The Address Operator & Pointer

All the variables are stored in the computer memory(RAM). If the RAM has K available bytes, they can be numbered as 1,2,3,…K. Suppose a variable x is assigned a random integer number, then one declares it as an integer variable. The Compiler assigns 2 bytes of memory for its storage.

Pointers in C with examples | Pointer | C Tutorials
Pointers in C with examples | Pointer | C Tutorials
Natural Language C Language
Assign any random no to operand x  x = rand();
Display the value assigned to x  printf(“%d\n”,x);

Suppose a user knows the address in the RAM where x is stored, s/he can follow the following steps to display the value of x. One can write the steps in both the natural & C language.

Natural Language C Language
Assign any random no to operand x  x = rand();
Let a_x be the address of x  a_x = &x;
Display whatever is stored at address a_x  printf(“%d\n”,*a_x);

One may note the statement

a_x = &x;

Where & is the address operator. This can be used it in function scanf(). If x is a variable, then &x provides its address. but a_x is also a variable. It is necessary to declare it. There will be a compilation error, if one decalres it as integer, unsigned integer or even long integer. This is a special type of variable which can store the address of another variable so it is known as a “Pointer Variable”.

printf(“%d\n”,*a_x);   /* It will display the value of x. */

Where a_x is a pointer variable and *a_x gives whatever is stored at location a_x. Since a_x is the address of variable x, then *a_x will give the value of x.

Leave a Reply

Your email address will not be published. Required fields are marked *