What is pointer variable? How its declared? How its address is determined? How pointer can be used to pass an entire array to a function in C? 10m Dec 2009

By | August 6, 2016

What is pointer variable? How is a pointer variable declared? How is the address of a pointer variable determined? How pointer can be used to pass an entire array to a function in C? Explain with the help of an example. 10

An ordinary variable is a location in memory that can hold a value. For example, when you declare a variable num as an integer, the compiler sets aside 2 bytes of memory to hold the value of the integer. In your program, you refer to that location in memory by the name num. At the machine level that location has a memory address.

int num = 100;

We can access the value 100 either by the name num or by its memory address. Since addresses are simply digits, they can be stored in any other variable. Such variables that hold addresses of other variables are called Pointers. In other words, a pointer is simply a variable that contains an address, which is a location of another variable in memory.

 

A pointer variable “points to” another variable by holding its address. Since a pointer holds an address rather than a value, it has two parts. The pointer itself holds the address. That addresses points to a value. There is a pointer and the value pointed to. This fact can be a little confusing until you get comfortable with it, but once you get familiar with it, then it is extremely easy and very powerful.

We have seen in the previous section that &qty returns the address of qty and this address can be stored in a variable as shown below:

ptr = &qty;

In C, every variable must be declared for its data type before it is used. Even this holds good for the pointers too. We know that ptr is not an ordinary variable like any integer variable. We declare the data type of the pointer variable as that of the type of the data that will be stored at the address to which it is pointing to. Since ptr is a variable, which contains the address of an integer variable qty, it can be declared as:

int *ptr;

where ptr is called a pointer variable. In C, we define a pointer variable by preceding its name with an asterisk (*). The “*” informs the compiler that we want a pointer variable, i.e. to set aside the bytes that are required to store the address in memory.

The int says that we intend to use our pointer variable to store the address of an integer.

 

Example

int x; /* x is initialized to a value 10*/

p = &x; /* Pointer declaration & Assignment */

*p=10;

Let us write the complete program as shown below:

# include <stdio.h>

main( )

{

int *p; /* a pointer to an integer */

int x;

p = &x;

*p=10;

printf(“The value of x is %d”,*p);

printf(“\nThe address in which the x is stored is %d”,p);

}

 

OUTPUT

The value of x is 10

The address in which the x is stored is 52004

 

Characteristic features of Pointers:

With the use of pointers in programming,

  1. The program execution time will be faster as the data is manipulated with the help of addresses directly.
  2. Will save the memory space.
  3. The memory access will be very efficient.
  4. Dynamic memory is allocated.

 

3. (b) Write a program in ‘C’ to find whether a given number is Armstrong number or not. 10

 Similar Solved program can be found on this link http://cssimplified.com/c-programming/a-c-program-to-find-all-armstrong-numbers-in-the-range-of-0-to-999

 

4. (a) Write program in ‘C’ for the multiplication of two square matrices. 10

Similar Solved program can be found on this link http://cssimplified.com/c-programming/an-interactive-c-program-to-multiply-two-matrices