Pointers in c

pointer is a very powerful and sophisticated feature provided in the C language. A variable defined in a program the compiler allocates a space in the memory to store its value. The number of bytes allocated to the variable depends on its type. For instance, a character is allocated 1 byte, an int is, in general, allocated 4 bytes, and a float is also allocated 4 bytes on a typical 32-bit system. The memory in RAM is grouped into bytes; each byte has 8 bits of memory. Bytes are sequentially numbered. Thus, each byte is associated with a number which is its address. When a variable is declared, a block of memory is allocated to store its value. The address of a variable is the byte number of the first byte of the memory block allocated for value storage. The value of a pointer to the variable is also the address where the value is stored, i.e., its value is the byte number of the first byte of the memory block where the value of variable is stored. A pointer to a data item is nothing but the address of that data item. Address of a variable may be determined by application of address-of operator (&).We can store this address in a variable, called the pointer variable, and use it to manipulate that data item. For instance, let Marks be a variable of type int. On a 32-bit system, Marks would be allocated a memory block of size 4 bytes for storing its value.

It may be noted that during its lifetime, a pointer variable need not always point to the same variable. We can manipulate the pointer itself so that it points to other variables. This is particularly useful while working with arrays. For example, the ++ operator increments the address stored in a pointer such that it points to the next element in that array. Similarly, the -- operator decrements a pointer so that it points to the previous array element. The use of pointers with arrays often leads to concise and efficient code.

There are several advantages in using pointers, some of which are listed below.

1. Pointers enable us to use call by reference mechanism. This enables changes to the formal parameters within a function to be reflected in arguments in the function call. Thus, the modified values are passed back to the calling function.

2. The use of pointers for manipulations of arrays and strings often leads to concise and efficient programs.

3. Using pointers, we can construct advanced data structures such as linked lists, trees, graphs, etc. to store and manipulate complex data. The use of such advanced data structures often leads to efficient programs.

4. Pointers permit more efficient use of memory, which is a very valuable and limited resource, using a technique called dynamic memory management.

5. Void pointers and pointers to functions enable us to write powerful generic functions that work with different data types.

6. Pointers also enable the command-line arguments to be passed to a program.

Illustrates pointers and the address-of operator (&)

#include <stdio.h>
int main ()
{
     int y = 78;
     int *ptry = &y, *P;
     clrscr();
     P = &y;
     printf ("Address of y = %p\n", &y);
     printf("ptry = %p\tP = %p\n", ptry, P);
     printf("*ptry = %d\n*P = %d\n", *ptry, *P);
     printf("*&y = %d \n", *&y);
     return 0;
}
Posted on by