Unit
Unit
ENGINEERING COLLEGE
(AUTONOMOUS | VISAKHAPATNAM)
• Example
int var = 10;
int *ptr;
ptr = &var;
• Now iptr contains the address of variable age i.e. it points to variable age,
similarly fptr points to variable sal. Since iptr is declared as a pointer of
type int, we should assign address of only integer variables to it. If we
assign .address of some other data type then compiler won't show any error
,but the output will be incorrect.
(i) x =*ptr++;
The expression *ptr++ is equivalent to *(ptr++), since these operators associate
from right to left. Hence the increment operator will be applied to ptr, and not to
*ptr. The increment operator is postfix, so first the value of ptr will be used in the
expression and then it will be incremented. Hence firstly the integer pointed to by
ptr will be dereferenced and assigned to x and then ptr will be incremented. This is
same as-
x = *ptr;
ptr = ptr + 1;
Value of x = 25, Address contained in ptr = 2002, *ptr = 38
Introduction to Programming Raghu Engineering College (A) 33
ii) x = *++ptr;
The expression *++ptr is equivalent to *(++ptr). Here also the increment operator is
applied to ptr. The increment operator is prefix, so first rtr will be incremented and
then its new value will be used in the expression. Hence firstly the value of ptr is
incremented, then value at the new address is dereferenced and assigned to x. This is
same as-
ptr = ptr+ 1;
x = *ptr;
Value of x = 38, Address contained in ptr = 2002, *ptr = 38
iii) x = ++*ptr;
The expression ++*ptr is equivalent to ++(*ptr). Here the increment operator is
applied over *ptr and not ptr. So here the value of pointer will not change but the
value pointed to by the pointer will change i.e., *ptr will increment. Since the
increment operator is prefix hence first the value of *ptr will increment and then this
value will be assigned to x. This is same as-
*ptr = *ptr + 1;
x = *ptr;
Value of x= 26, Address contained in ptr = 2000, *ptr = 26
Introduction to Programming Raghu Engineering College (A) 34
iv) x = (*ptr)++;
. Here also the increment operator is applied over *ptr and since it is postfix
increment hence first the value of *ptr will be assigned to x and then it will be
incremented. This is same as-
x = *ptr;
*ptr = *ptr+1;
Value of x = 25, Address contained in ptr = 2000, *ptr = 26
#include <stdio.h>
int main() {
int *ptr = NULL; // Initializing a pointer with NULL
int value = *ptr; // Attempting to dereference a null pointer, This will result in undefined
//behavior
printf("Value: %d\n", value); // The following line might cause a crash or unexpected
//behavior
return 0;
}
• Here 5000 is the address of first element, and since each element (type int)
takes 2 bytes so address of next-element is 5002, and so on. The address of
first element of the array is also known as the base address of the array.
Introduction to Programming Raghu Engineering College (A) 41
• The name of the array 'arr' denotes the address of 0th element of array
which is 2000. The address of 0th element can also be given by &arr[0],
so arr and &arr[0] represent the same address.
• The name of an array is a constant pointer, and according to. pointer
arithmetic when an integer is added to a pointer then we get the address
of next element of same base type. Hence (arr+1) will denote the address
of the next element arr[l]. Similarly (arr+2) denotes the address of arr[2]
and so on.
#include<stdio.h>
int main ( )
{
int arr[5]={5,10,15,20,25};
int i;
for(i=0;i<5;i++)
{
printf ("Value of arr [%d] = %d\t",i,*(arr+i));
printf("Address of arr[%d] = %u\n",i, arr+i);
}
return 0;
}
The following figure shows how the above 2-D array will be stored in memory.
• In general we can write, arr+i Points to ith element of arr Points to ith 1-D array.
• *(arr+i) gives us the base address of ith 1-D array.
• In general we can write- *(arr+i), arr[i] - Base address of ith l-D array - Points to 0th
element of ith 1-D array
int main(int argc, char *argv[]) int main(int argc, char **argv)
{ {
/* ... */ or /* ... */
} }