When the
pointer is incremented by an increment operator, it is always right
incremented. That is, if the pointer points to an integer, the pointer is
incremented by 2, and, if it is long, it is incremented by 4.
Program
#include <stdio.h>
void printarr(int a[]);
void printdetail(int a[]);
void print_usingptr(int a[]);
main()
{
int a[5];
for(int i
= 0;i<5;i++)
{
a[i]=i;
}
print_usingptr(a);
}
void printarr(int a[])
{
for(int i
= 0;i<5;i++)
{
printf("value in array %d\n",a[i]);
}
}
void printdetail(int a[])
{
for(int i
= 0;i<5;i++)
{
printf("value in array %d and address is
%8u\n",a[i],&a[i]);
}
}
void print_usingptr(int a[])
{
int *b;
b=a;
for(int i
= 0;i<5;i++)
{
printf("value in array %d and address is %16lu\n",*b,b);
b++; // A
}
}
Explanation
- This function is similar to the preceding
function except for the difference at statement A. In the previous
version, b =
b+2 is
used. Here b++ is used to increment the
pointer.
- Since the pointer is a
pointer to an integer, it is always incremented by 2.
Point to Remember
The
increment operator increments the pointer according to the size of the data
type.
You can
put values in the memory locations by using pointers, but you cannot assign the
memory location to an array to access those values because an array is a
pointer constant.
Program
#include <stdio.h>
void printarr(int a[]);
void printdetail(int a[]);
void print_usingptr_a(int a[]);
main()
{
int a[5];
int *b;
int *c;
for(int i
= 0;i<5;i++)
{
a[i]=i;
}
printarr(a);
b++; \\ B
*b=4; \\ C
b++;
*b=6; \\ D
b++;
*b=8; \\ E
b++;
*b=10;
b++;
*b=12;
b++;
a=c;
//error \\F
printarr(a);
}
void printarr(int a[])
{
for(int i
= 0;i<5;i++)
{
printf("value in array %d\n",a[i]);
}
}
void printdetail(int a[])
{
for(int i
= 0;i<5;i++)
{
printf("value in array %d and address is
%16lu\n",a[i],&a[i]);
}
}
void print_usingptr_a(int a[])
{
for(int i
= 0;i<5;i++)
{
printf("value in array %d and address is %16lu\n",*a,a); \\ F
a++;
// increase by 2 bytes \\ G
}
}
Explanation
- You can assign a value at the location
specified by b using statement A.
- Using statement B, you can
point to the next location so that you can specify a value at that
location using statement C. Using this procedure, you can initialize 5
locations.
- You cannot assign the
starting memory location as given by statement F to access those elements
because a is a pointer constant and
you cannot change its value.
- The function print_usingptr_a works correctly even though
you are writing a++. This is because when you
pass a as a pointer in an actual
parameter, only the value of a is passed and this value is copied to the
local variable. So changing the value in the local variable will not have
any effect on the outside function.
Point to Remember
The array
limit is a pointer constant and you cannot change its value in the program.
You can
define two- or multi-dimensional arrays. It is taken as an array of an array.
Logically, the two-dimensional array 3 X 2 is taken as
3 1
5 2
8 7
Here
there are three arrays, i.e. one array in each row. The values are stored as
3 1 5
2 8 7
This
style is called row measure form. Each row array is represented as a[0], which consists of elements 3 and 1. a[1] consists of 5 2 and a[2] consists of 8 7. Each element of a[0] is accessed as a [0] [0] and a[0] [1], thus
the value of a[0][0] and a[0][1] is 1.
Program
#include <stdio.h>
void printarr(int a[][]);
void printdetail(int a[][]);
void print_usingptr(int a[][]);
main()
{
int
a[3][2]; \\ A
for(int i
= 0;i<3;i++)
for(int j=0;j<2 ;j++)
{
{
a[i]=i;
}
}
printdetail(a);
}
void printarr(int a[][])
{
for(int i
= 0;i<3;i++)
for(int
j=0;j<2;j++)
{
{
printf("value in
array %d\n",a[i][j]);
}
}
}
void printdetail(int a[][])
{
for(int i
= 0;i<3;i++)
for(int j=0;j<2;j++)
{
{
printf(
"value in array %d
and address is %8u\n",
a[i][j],&a[i][j]);
}
}
}
void print_usingptr(int a[][])
{
b=a; \\ C
for(int i
= 0;i<6;i++) \\ D
{
printf("value in array %d and address is %16lu\n",*b,b);
b++;
// increase by 2 bytes \\ E
}
}
Explanation
- Statement A declares a
two-dimensional array of the size 3 × 2.
- The size of the array is 3 ×
2, or 6.
- Each array element is
accessed using two subscripts.
- You can use two for loops to access the array.
Since i is used for accessing a
row, the outer loop prints elements row-wise, that is, for each value of i, all the column values are
printed.
- You can access the element
of the array by using a pointer.
- Statement B assigns the base
address of the array to the pointer.
- The for loop at statement C
increments the pointer and prints the value that is pointed to by the
pointer. The number of iterations done by the for loop, 6, is equal to the
array.
- Using the output, you can
verify that C is using row measure form for storing a two-dimensional
array.
Points to Remember
- You can define a
multi-dimensional array in C.
- You have to provide multiple
subscripts for accessing array elements.
- You can access array
elements by using a pointer.