Declaring a Pointer - Devbhoomi

FREE JOB ALERT & ONLINE TUTORIALS

Hot

Post Top Ad

Sunday 5 November 2017

Declaring a Pointer

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 i i.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 j contains 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).

No comments:

Post a Comment

Post Top Ad