C Pointer Address

Syntax

Get the address of a variable, use & operator.

Example

Define a pointer: include a * before the name of the variable. Get the address: use &.


#include <stdio.h>
/*from w w w . j a  v  a 2 s .  c  o m*/
main ()
{
   int i;        
   int *ia;     
   i = 10;       
   ia = &i;  

    printf (" The address of i is %8u \n", ia);         
    printf (" The value at that location is %d\n", i);  
    printf (" The value at that location is %d\n", *ia);
    *ia = 50;                               
    printf ("The value of i is %d\n", i);              
}

The code above generates the following result.

Example 2

Print addresses using place holders %16lu or %p.


#include <stdio.h>
//from w  ww  .j a v a2 s .co  m
main()
{
    int a[5];
    int i;
    for(i = 0;i<5;i++)
    {
        a[i]=i;
    }

    for(i = 0;i<5;i++)
    {
        printf("value in array %d and address is %16lu\n",a[i],&a[i]);
    }

   for(i = 0;i<5;i++)
    {
        printf("value in array %d and address is %p\n",a[i],&a[i]);
    }
}

The code above generates the following result.





















Home »
  C Language »
    Language Advanced »




File
Function definition
String
Pointer
Structure
Preprocessing