Pointer Fundamentals
■ When a variable is defined the
compiler (linker/loader actually) 00000000
allocates a real memory address 00000000
for the variable. x
00000000
■ int i; will allocate 2 bytes in
the main memory, which will be 00000011
used to store an integer value.
■ When a value is assigned to a
variable, the value is actually
placed to the memory that was
allocated.
■ i=3; will store integer 3 in the
2 bytes of memory.
Pointers
■ When the value of a variable is 00101000
used, the contents in the x
01001010
memory are used.
■ y=i; will read the contents
in the 2 bytes of memory,
and then assign it to variable
y.
■ &i can get the address of i.
(referencing operator &)
■ The address can be passed to a y
function:
■ scanf("%d", &i);
■ The address can also be stored
in a variable ……
Pointers
■ To declare a pointer variable
type * pointername;
❑ Why we use pointer variable
■ For example:
■ int * p1; p1 is a variable that tends to point to
an integer, (or p1 is a int pointer)
■ char *p2;
■ unsigned int * p3;
■ p1 = &x; /* Store the address in p1 */
■ scanf("%d", p1); /* i.e. scanf("%d",&x); */
Initializing Pointers
■ Like other variables, always initialize pointers before
using them!!!
■ For example:
int main(){
int x;
int *p; Don’t
scanf("%d",p); /* */
p = &x;
scanf("%d",p); /* Correct */
}
Using Pointers
■ You can use pointers to access the values of other
variables, i.e. the contents of the memory for other
variables.
■ To do this, use the * operator (indirection operator).
■ Depending on different context, * has different
meanings.
■ For example:
int n, m=3, *p;
p=&m;
n=*p;
printf("%d\n", n);
printf("%d\n",*p);
Pointers in C
■ Declare pointers to any data type using operator *
int *int_ptr;
char *ch_ptr;
float *float_ptr,
■ A pointer takes its value as the address in memory of
another variable
int a = 5 a 6
int *a_ptr;
a_ptr = &a;
/* ‘&’ : address of operator */ a_ptr address
of ‘a’
(*a_ptr) ++ ;
/* ‘*’ : dereference operator */
Pointer example
Pointer Expression
■ j=&i;
Here j is not an ordinary variable like any other integer
variable.
It is a variable which contain the address of another
variable. ( i )
Ex.
i=3;
j=&i; // j’s value is address of i.
j is a variable which contains the address of i.
int *j;
Means value at the address contained in j as an integer.
i j
3 6485
6485 3276
Ex.
■ Main()
{
int i=3;
int *j;
j=&i;
printf(“\n Address of i=%u”,&i);
printf(“\n Address of j=%u”,&j);
printf(“\n Value of j=%d” ,j);
printf(“\n Value of i=%d”, i);
printf(“\n Value of i=%d”,*(&i));
printf(“\n Value of i=%d”,*j);
}
■ OUTPUT:
Address of i 6485
Address of j 3276
Value of j 6485
Value of i 3
Value of i 3
Value of i 3
i j k
3 6485 3276
6485 3276 7234
main( )
{
int i=3;
int *j;
int **k;
j=&i;
k=&j;
printf(\n Address of i=%u” , &i);
printf(\n Address of i=%u” , j);
printf(\n Address of i=%u” , *k);
printf(\n Address of j=%u” , &j);
printf(\n Address of j=%u” , k);
printf(\n Address of k=%u” , &k);
printf(“\n ----------------------”);
printf(\n value of j=%u” , j);
printf(\n value of k=%u” , k);
printf(\n value of i=%d” , i);
printf(\n value of i=%d” ,*(&i));
printf(\n value of i=%d” , *j);
printf(\n value of i=%d” , **k);
}
■ Address of i= 6485
Address of i= 6485
Address of i= 6485
Address of j= 3276
Address of j= 3276
Address of k=7234
----------------------
value of j= 6485
value of k= 3276
value of i=3
value of i=3
value of i=3
value of i=3
Pointer expression
■ float *s;
■ char *ch;
means value at address stored in ch is
going to be a char.
Passing pointer as argument to a
function
main()
{
int a=5;
void sum (int *b);
printf(“ Now value of a will be incremented”);
sum (&a);
}
void sum (int *x)
{
*x=*x+5;
printf(“%d”,*x);
}
Pointer to function
int * sum (int , int)
main()
{
int a,b;
int *x;
printf(“\n Enter the value of a”);
scanf(“%d”,&a);
printf(“\n Enter the value of b”);
scanf(“%d”, &b);
x=sum(a,b);
printf(“\n The addition of two no.s is %d”, *x);
}
int *sum(int x, int y)
{
int *c, p;
p=x+y;
c=&p;
return c;
}
Pointer to Array
■ Accessing array element using pointer
main()
{
int num[ ]={24,34,12,44,56,17};
int i=0,*j;
j=&num[0];
while(i<=5)
{
printf(“\n Address = %u”,&num[i]);
printf(“Element=%d”, *j);
i++;
j++;
}
}
Address = 100 Element =24
Address = 102 Element =34
Address = 104 Element =12
Address = 106 Element =44
Address = 108 Element =56
Address = 110 Element =17
Passing Entire Array to function:
main()
{
int num[ ]={24,34,12,44,56,17};
display(&num[0],6);
}
display(int *j,int n)
{
int I;
for(i=0;i<n;i++)
{
printf(“element= %d”,*j);
j++;
}
■ int num[]={24,34,12,44,56,17}
■ When we refer name of array we get it’s base
address.
■ *num:refer to zeroth element of the array
■ *num=*(num+0)
■ *(num +1):refer to first element
■ When we will write num[i] compiler converts it
to *(num+i)
■ So
■ num[i]
■ *(num+i)
■ *(i+num)
■ i[num] all are same.
Real Thing
main()
{
int num[ ] ={23,34,12,44,56,17};
int i=0;
while(i < =5)
{
printf (“\n address =%u “ ,&num[i]);
printf (“Elements = %d”, num[i]);
printf(“%d”,*(num+i);
printf(“%d”,*(i+num);
printf(“%d” ,i [num]);
i++;
}
}
o/p:
■ Address=100 Elements 23 23 23 23
■ Address=102 elements 34 34 34 34
■ Address=104 Elements 12 12 12 12
■ Address=106 elements 44 44 44 44
■ Address=108 Elements 56 56 56 56
Address=110 elements 17 17 17 17