bscodinglab

Pointer / Memorry Address

  • A pointer is a variable that stores the memory address of another variable as its value.
  • A pointer variable points to a data type (like int) of the same type, and is created with the * operator.
  • The address of the variable you are working with is assigned to the pointer:

            Syntex :-  type *var-name;

Here, type is the pointer’s base type; it must be a valid C data type and var-name is the name of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.

 

Take a look at some of the valid pointer declarations.

                         int    *ip;    /* pointer to an integer */

                        double *dp;    /* pointer to a double */

                        float  *fp;    /* pointer to a float */

                        char   *ch     /* pointer to a character */

 

How to Use Pointers?

There are a few important operations, which we will do with the help of pointers very frequently. (a) We define a pointer variable, (b) assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand.

The following example makes use of these operations

#include <stdio.h>

int main ()

 {

             int  var = 20;   /* actual variable declaration */

            int  *ip;        /* pointer variable declaration */

             ip = &var;  /* store address of var in pointer variable*/

             printf(“Address of var variable: %x\n”, &var  );

             /* address stored in pointer variable */

            printf(“Address stored in ip variable: %x\n”, ip );

             /* access the value using the pointer */

            printf(“Value of *ip variable: %d\n”, *ip );

             return 0;

}

 

When the above code is compiled and executed, it produces the following result.

  • Address of var variable: bffd8b3c
  • Address stored in ip variable: bffd8b3c
  • Value of *ip variable: 20
  • NULL Pointers

It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.

The NULL pointer is a constant with a value of zero defined in several standard libraries. Consider the following program :-

            #include <stdio.h>

            int main ()

             {

                         int  *ptr = NULL;

                        printf(“The value of ptr is : %x\n”, ptr  );

                        return 0;

            }

When the above code is compiled and executed, it produces the following result .

The value of ptr is 0

In most of the operating systems, programs are not permitted to access memory at address 0 because that memory is reserved by the operating system. However, the memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location. But by convention, if a pointer contains the null (zero) value, it is assumed to point to nothing.

To check for a null pointer, you can use an ‘if’ statement as follows.

            if(ptr)     /* succeeds if p is not null */

            if(!ptr)    /* succeeds if p is null */

 

Pointers in Detail

Pointers have many but easy concepts and they are very important to C programming. The following important pointer concepts should be clear to any C programmer .

 

Sr.No.

Concept

Description

1

Pointer arithmetic

There are four arithmetic operators that can be used in pointers: ++, –, +, –

2

Array of pointers

You can define arrays to hold a number of pointers.

3

Pointer to pointer

C allows you to have pointer on a pointer and so on.

4

Passing pointers to functions in C

Passing an argument by reference or by address enable the passed argument to be changed in the calling function by the called function.

5

Return pointer from functions in C

C allows a function to return a pointer to the local variable, static variable, and dynamically allocated memory as well.

 

Pointer arithmetic :-

A pointer in c is an address, which is a numeric value. Therefore, you can perform arithmetic operations on a pointer just as you can on a numeric value. There are four arithmetic operators that can be used on pointers: (++, –, +, and – ) . Address arithmetic is a method of calculating the address of an object with the help of arithmetic operations on pointers and use of pointers in comparison operations. Address arithmetic is also called pointer arithmetic.

Example of Decriments Operator  :-

                        #include <stdio.h>

                        const int MAX = 3;

                        int main () {

int  var[] = {10, 100, 200};

 int  i, *ptr;

/* let us have array address in pointer */

ptr = &var[MAX-1];

for ( i = MAX; i > 0; i–)            {

            printf(“Address of var[%d] = %x\n”, i-1, ptr );

            printf(“Value of var[%d] = %d\n”, i-1, *ptr );

            /* move to the previous location */

            ptr–;    }

 return 0;

}

Output :- 

            Address of var[2] = bfedbcd8

Value of var[2] = 200

Address of var[1] = bfedbcd4

Value of var[1] = 100

Address of var[0] = bfedbcd0

Value of var[0] = 10

Array of pointers :-

An array of pointers is an array that consists of variables of pointer type, which means that the variable is a pointer addressing to some other element.

An array name contains the address of first element of the array which acts like constant pointer. It means, the address stored in array name can’t be changed. For example, if we have an array named val then val and &val[0] can be used interchangeably.

Example :-

#include<stdio.h>

#define size 5

int main()

{

    int *arr[size];

    int a = 10, b = 20, c = 30, d = 40, e = 50, i;

    arr[0] = &a;    arr[1] = &b;    arr[2] = &c;    arr[3] = &d;    arr[4] = &e;

    printf(“Address of a = %p\n”,arr[0]);

    printf(“Address of b = %p\n”,arr[1]);

    printf(“Address of c = %p\n”,arr[2]);

    printf(“Address of d = %p\n”,arr[3]);

    printf(“Address of e = %p\n”,arr[4]);

    for(i = 0; i < size; i++)

     printf(“value stored at arr[%d] = %d\n”,i,*arr[i]);

    return 0;

}

 

 

Output :-

Address of a = 0x7ffec80c8aac

Address of b = 0x7ffec80c8aa8

Address of c = 0x7ffec80c8aa4

Address of d = 0x7ffec80c8aa0

Address of e = 0x7ffec80c8a9c           value stored at arr[0] = 10

value stored at arr[1] = 20

value stored at arr[2] = 30

value stored at arr[3] = 40

value stored at arr[4] = 50

 

Pointer to pointer :-

As we know that, a pointer is used to store the address of a variable in C. Pointer reduces the access time of a variable. However, In C, we can also define a pointer to store the address of another pointer. Such pointer is known as a double pointer (pointer to pointer). The first pointer is used to store the address of a variable whereas the second pointer is used to store the address of the first pointer. Let’s understand it by the diagram given below.

Example :-

#include<stdio.h> 

void main () 

    int x = 25; 

    int *p; 

    int **pp;  

    p = &x; // pointer p is pointing to the address of a 

    pp = &p; // pointer pp is a double pointer pointing to the address of pointer p 

    printf(“address of a: %x\n”,p); // Address printed  

    printf(“address of p: %x\n”,pp); // Address printed 

    printf(“value stored at p: %d\n”,*p); // value stoted on address contained by p i.e. 10 will be printed 

    printf(“value stored at pp: %d\n”,**pp); // value stored on address contained by pointer stoyred at pp 

Output :-    address of a: da5b7e54

address of p: da5b7e48

value stored at p: 25

value stored at pp: 25

Passing pointers to functions in  C:-

In this tutorial, you will learn how to pass a pointer to a function as an argument. To understand this concept you must have a basic idea of Pointers and functions in C programming.

In this example, we are passing a pointer to a function. When we pass a pointer as an argument instead of a variable then the address of the variable is passed instead of the value. So any change made by the function using the pointer is permanently made at the address of passed variable. This technique is known as call by reference in C.

Example :-

#include <stdio.h>

void salaryhike(int  *var, int b)

{

    *var = *var+b;

}

int main()

{

    int salary=0, bonus=0;

    printf(“Enter the employee current salary:”);

    scanf(“%d”, &salary);

    printf(“Enter bonus:”);

    scanf(“%d”, &bonus);

    salaryhike(&salary, bonus);

    printf(“Final salary: %d”, salary);

    return 0;

}

Output :-  Enter the employee current salary:50

                           Enter bonus:10

                           Final salary: 60

Return pointer from functions in C :-  C programming allows to return an array from a function. Similarly, C also allows to return a pointer from a function. We have to declare a function returning a pointer as to given  Syntex.

Syntex :-  int  * myFunction ()  {   .   .   .  }

 

Example :-

// C program to illustrate the concept of  Returning pointer from a function

#include <stdio.h>

  // Function returning pointer

int*  fun()

{

    int A = 10;

    return (&A);

}

  // Driver Code

int main()

{

    // Declare a pointer

    int* p;

      // Function call

    p = fun();

      printf(“%p\n”, p);

    printf(“%d\n”, *p);

    return 0;

}

Run Time Error :-  Segmentation fault (core dumped)

Output :-  No  Output this Example