Monday, 24 July 2017

Pointer in C-language

Pointer is a variable that points to an address of a value.
Pointer => address that contains the value

Symbols used in pointer :
& (ampersand sign) : ‘Address of operator’. It determines the address of a variable.
* (asterisk sign) : indirection operator / value at address. Accesses the value at the address.
Example :

int i = 3 ;

This declaration tells the C compiler to :-
  • Reserve space in memory to hold the integer value.
  • Associate the name i with this memory location.
  • Store the value 3 at this location.

We may represent the location of i in the memory by :-
  i-------location name
 3--------value at location
 65524-------location number

  • The computer has selected memory location 65524 as the place to store the value 3.
  • The location number 65524 is not a number to be relied upon, because some other time the computer may choose a different location for storing the value 3.
  • The important point is, i’s address in memory is a number.

Example :

#include <stdio.h>
#include <conio.h>

    void main()
    {
        int i = 3 ;
        printf ( "\nAddress of i = %u", &i);
        printf ( "\nValue of i = %d", i);
        printf ( "\nValue of i = %d", *(&i));
    }

Output :
Address of i = 65524
Value of i = 3

Explanation :
  • The pointer points to the address 65524 of variable i, that contains the value of i i.e 3
  • Pointer points to ->65524 address that points to =>value 3
  • &i gives the address of the variable i.
  • *i gives the value store at the address of i.
  • ‘&’ => ‘address of ’ operator.
  • The expression &i in the first printf() statement returns the address of the variable i, which in this case happens to be 65524.
  • As 65524 is an address, there is no sign associated with it. So to print the address we use ‘%u’ which is a format specifier for printing an unsigned integer.
  • The another pointer operator is ‘*’ called ‘value at address’ operator which gives the value stored at a particular address.
  • The ‘value at address’ operator is also called ‘indirection’ operator.
  • *(&i) is same as printing the value of i.

Syntax :

datatype *variable_name;  //pointer to datatype


Example :

int *j; //pointer to integer

  • This means the value at the address contained in j is an int.
  • This declaration tells the compiler that j will be used to store the address of an integer value.
  • The j is called as pointer variable. This variables are capable of holding addresses.

  • These addresses can be collected in a variable
    j = &i ;
  • The variable j is called pointer variable that stores the address of i i.e 65524
  • j(contains the address of i) points to -> the address of i , this points to => the value of ii.e 3but the address of j is 65522.


#include <stdio.h>
#include <conio.h>
void main()
{
    int i = 3;
    // store the address of an integer value
    int *j;

    clrscr();

    //stores the address of i variable
    j=&i;
    printf("Address of i variable is %x \n",&i);
    printf("Address of j variable is %x \n",j);
    printf("Value of j variable is %d \n",*j);

    getch();
}


Output :
Address of i variable is 65524
Address of j variable is 65522
Value of j variable is 3

The Expression *j will give the value of i i.e 3
As * stands for ‘value at address’. Means the jcontains the address of variable i, so *j will give the value stored at that address which is there in j (i.e 65524 address contains value 3).





Why to declare pointer:::::

int *i;
char *ch;
float *f;

  • Here, ich and f are declared as pointer variables, i.e.variables capable of holding addresses.
  • Remember that, addresses are always going to be whole numbers, therefore pointers always contain whole numbers.
  • Now we can put these two facts together and say that the pointers are variables that contain addresses, and since addresses are always whole numbers, pointers would always contain whole numbers.
  • The declaration float *f does not mean that f is going to contain a floating-point value. What it means is, f is going to contain the address of a floating-point value.
  • Similarly, char *ch means that ch is going to contain the address of a char value.







No comments:

Post a Comment