C-Programming

Pointers

Pointer is an important and powerful feature of C programming which has made the C more user friendly. At the initial condition, it might be difficult to understand the pointer. Pointer is an address variable that contains/holds the address of another variable.  The address is the location of another variable in the memory. For example if one variable contains the address of another variable then the first variable is said to be point to the second variable. There are many reasons for using the pointer, some of which are as follows:

  1. A pointer allows passing variables, arrays, functions, strings, and structures as function argument.
  2. It supports dynamic memory allocation and de-allocation of memory segments.
  3. With the help of pointer, variable can be swapped without physically moving them.
  4. It allows establishing links between data elements for complex data structures such as link lists, stacks, queues, trees, and graphs.
  5. Since pointer is directly related to the address of the variable, so the efficiency or speed of the execution of the program will be increased.
  6. Pointers provide a way to return multiple data items from a function via function arguments.
  7. Pointers are closely related with arrays and therefore provide an alternative way to access individual array elements. This means we can use pointer instead of using array in the program.
  8. The use of pointer reduces the length of program.
  9. Pointer can return more than one values.

A pointer contains the memory address. Most commonly this is the location of another variable, where it has been stored in memory. If one variable contains the address of another variable then the first variable is said to be pointed to the second one. In C pointers are distinct such as integer pointer, character pointer; floating point number pointer etc. a pointer consists of two parts which are:

  1. The pointer operator
  2. The address operator

  1. The pointer operator:

A pointer operator can be represented by combination of *(asterisk) with variable. For example, if a variable of integer data type and also declared *(asterisk) with another variable. It means the variable is of type “pointer to integer”. In other words, it will be used in the program indirectly to access the value of one or more integer variables.

      Declaration of the pointer variable:

Syntax:

Data_type  *pointer_variable_name;

For example: int *ptr;

Here prt is pointer which holds the address of an integer data type. All pointer variables must be declared before they are used in the programs like other variables. When a pointer variable is declared, an asterisk sign i.e. * must precede the variable name, this identifies the variable as a pointer.

  1. Address operator:

 An address operator can be represented by a combination of ampersand sign (&) with a pointer variable. For example, if a pointer variable is an integer type and also declared ‘&’ with the pointer variable then it means that the variable is of type “address of”. In other words, it will be used in the programs to indirectly access the value of one or more integer variables. The ‘&’ is unary operator that returns the memory address of its operand. For example:

Add=&ptr;

Here the pointer operator & is an operator that returns the address of the variable following it. Therefore the preceding assignment statement could be verbalized as “in receives the address of ptr”. In the example, &ptr represents the address of the ptr which is assigned to the Add.

The other operator * is the complement of &. It is unary operator that returns the value of the variable that is located at the address that follows.

The operation of * implies to the phrase “at address”. Unfortunately, the symbol * represents both the multiplication sign and the “at address”, and the symbol & represents both bit wise AND and the “address of” sign. When these are used as pointer operators, they have no relationship to the arithmetic operators. That happened to look like the same. Both, the pointer operators & and * have higher precedence over all other operators except the unary minus, with which they have equal precedence.

Assigning the pointer variable

A pointer is a variable data type and hence the general rule to assign its value to the pointer is same as that of any other variable data type.

For example:

int a=10,b;

int *ptr1, *ptr2;

  • ptr=&a;

Here the address of ‘a’ i.e. 10 is assigned to the pointer variable ptr.

  • b=*ptr1;

here *ptr represents the content of the ptr. Hence the content of the pointer variable ptr1 is assigned to the variable b, not the memory address.

  • ptr2=ptr1;

Here the address of the ptr2 is assigned to the pointer variable ptr2. the contents of both ptr1 and ptr2 will be same as these two variables hold the same address.

A program to display the contents of the pointer.

#include<stdio.h>

#include<conio.h>

main()

{

int x;

int *ptr;

clrscr();

x=10;

ptr=&x;

printf(“x=%d and ptr=%d”,x,ptr);

printf(“\nx=%d and the contents of the ptr=%d”,*ptr);

getch();

}

A program to assign a character variable to the pointer and to display the contents of the pointer.

#include<stdio.h>

#include<conio.h>

main()

{

char x,y;

char *ptr;

clrscr();

x=’c’;  /* assignment of character*/

ptr=&x;

y=*ptr;

printf(“value of x=%c”,x);

ptintf(“\n Pointer value ptr=%c”,y);

getch();

}

A program to assign the pointer variable to another pointer and display  the contents of the pointer variable.

#include<stdio.h>

#include<conio.h>

main()

{

int x;

int *ptr1,ptr*2;

clrscr();

x=10;

ptr1=&x;

ptr2=ptr1

Important Questions from Functions (Prev Lesson)
(Next Lesson) String
Back to C-Programming

No Comments

Post a Reply

Teacher
Bhim Gautam
Role : Lecturer
Read More
error: Content is protected !!