What is the role of Pointers in C programming

Pointers in C

Variables are stores in memory, each memory location has a numeric address. Variable names in C and other high-level languages enable the programmer to refer to memory locations by name, but the compiler must translate these names into addresses, and this process being automatic, is transparent to the programmer.

Pointers in C
Pointers

however, the kind of problems for which  C is particularly noted requires that the user be able to refer to those addresses indirectly, in order to manipulate the contents of the corresponding memory locations. But then, why variable names are not sufficient to provide the kind of manipulations required in C.

Memory addresses are global to all functions, whereas local variable names are meaningful only within the function in which the y are declared. A function can pass the address of a local variable to another function, and the second function can use this address to access the contents of the first function's local variable.

The addresses are passed only to the functions that need to access those memory locations. Thus, the function possessing the address can access the variable, so the data is now protected against unauthorized access/manipulations. 

Each & every variable in the program will have a unique memory address, the local variables die when the function in which they are declared finish execution, and the memory location formerly occupied by such a local variable then is now free, and can be allocated to another local variable later in the program.

Within the computer's memory, every stored data item occupies one or more contiguous memory cells. The number of memory cells required to store a data item depend on the type of data item. E.g. a single character will typically be stored in 1 byte of memory, an integer requires 2 contiguous bytes, a floating-point number occupies 4 contiguous bytes, and son on.

Addresses can be stored in variables, addresses are a separate data type in C.

A Pointer is a variable that represents the location (rather than the value) of a data item, such as a variable or an array element.

A Pointer provides an indirect way of accessing the value of a  data item. A pointer is a variable which contains the address of another variable.

Pointers help in returning multiple data items from a function through function arguments.

Pointers also allow references to other functions to be specified as arguments to a given function, i.e, passing function as arguments to the given function.

Pointers provide an easy way to access individual array elements.

Pointers allow a collection of strings to be represented within a single array, even though the individual strings may differ in length.

Pointers provides a symbolic way of using addresses. As the hardware instructions of the computer use addresses extensively, pointers allow us to express ourselves as computers express themselves. This makes program with pointers efficient.

Suppose i is a variable storing some data-item, the compiler will automatically assign memory cells for this data-item. If we know the address of the first memory cell we can access the data-item. The address of i' s memory location can be determined by the expression & i, where & is a unary operator, called the address operator. If we assign the address to another variable j i.e,:

j = &i

This new variable is called a pointer to j, because it points to the location where i is stored in memory. Note that j represents i's address, not its value. Thus j is referred to as pointer variable. 

The relationship between i and j is as follows :

address of i  →  value of i

       j                     i


Program-1

#include<stdio.h>]

/*Relationship between tow integer variables, Their corresponding address & their associated pointers. */

void main()

{        int number =3;

          int variable;

          int *number_pointer;        /* Pointer to an integer */

          int *variable_pointer;        /* Pointer to an integer */

          number_pointer =&number;    /*Assign address of number to number_pointer */

          variable = *number_pointer;    /*Assign value of number to variable */

          variable_pointer = &variable;   /*Assign address of variable to variable_pointer */

          printf("\nNumber = %d  &Number = %X Number_pointer = %X

                *Number_pointer =%d", number, &number, number_pointer, *number_pointer);

           printf("\nVariable = %d  &Variable = %X Variable_pointer = %X

                *Variable_pointer =%d", variable, &variable, variable_pointer, *variable_pointer);

          }

 

Run 

Number = 3     &Number = FFF4 Number_pointer = FFF4 *Number_pointer =3

Variable = 3     &Variable = FFF4 Variable_pointer = FFF4 *Variable_pointer =3


The data item represented by variable (for example, the data item stored in variable's memory cell) can be accessed by the expression *variable_pointer, where * is a unary operator, called the indirection operator, that operates only on a pointer variable. Therefore *variable_pointer and variable both represent the same data item.

If we write variable_pointer = &variable and number = *variable_pointer, then number and variable will both represent the same value, for example, the value of variablewill indirectly be assigned to number.

The ampersand (&) preceding a variable name in a call to the scanf function returns the address of the variable associated. It may precede only a variable name or array element. But not to a constant, an expression, or the unsubscripted name of an array.


Program-2

#include<stdio.h>

/* Difference between *and & */

void main()

{        int numb=5;

          printf("\nThe Value of %d is stored at address %u\n",numb,&numb);

          printf("\nThe Value of %d is stored at address %u\n",numb,&numb);

         }

Run

The Value of 5 is stored at address 65524

The address of the variable numb (65524) can be obtained by prefixing numb with the reference operator & as &numb. In other words, the & returns the memory address of its operand. The reference operator is also called the address operator as it is used to obtain the address of the variable.

The second symbol is the indirection operator *. Its operand is the address of a memory location and it returns the value stored at that address. Since the expression &numb means "numb's address", the expression *(&numb) is "the value stored at numb's address", which is 500.

 

Exercise regularly. Eat a nutritious diet. Don't smoke - WHO (Covid-19).

 

Let's see also...

 

Thanks a lot for reading.
Neel Kamal
What is the role of Pointers in C programming What is the role of Pointers in C programming Reviewed by Neel Kamal on November 12, 2021 Rating: 5

No comments:

For More Details Subscribe & Comment..!

Powered by Blogger.