The concept of array is very much bound to the one of the pointers. An array occupies consecutive memory locations.
POINTERS
AND ARRAYS
The
concept of array is very much bound to the one of the pointers. An array
occupies consecutive memory locations. Consider Figure 7.2. For example, if we
have an array declared as
int arr[] = {1, 2, 3, 4, 5};
then in memory it would be stored as shown in Figure 7.2.3
Array
notation is a form of pointer notation. The name of the array is the starting
address of the array in memory. It is also known as the base address. In other
words, base address is the address of the first element in the array or the
address of arr [0] . Now let us use a pointer variable as given in the
statement below.
int *ptr;
ptr = &arr [0];
Here, ptr is made to point to the first
element of the array. Execute the code given below and observe the output which
will make the concept clear to you.
Programming Tip: The
name of the array is actually a pointer that points to the first element of the
array.
main()
{
int arr[]={1,2,3,4,5);
printf("\n Address of array =
%p %p %p", arr, &arr[0], &arr);
}
Similarly,
writing ptr = &arr [2], makes ptr to point to the third element of the
array that has index 2. Figure 7.3 shows ptr pointing to the third element of
the array.
If
pointer variable ptr holds the address of the first element in the array, then
the address of successive elements can be calculated by writing ptr++.
int *ptr = &arr [0];
ptr++;
printf("\n The value of the
second element of the array is %d", *ptr);
The
printf() function will print the value 2 because after being incremented ptr
points to the next location. One point to note here is that if x is an integer
variable, then x++ adds 1 to the value of x. But ptr is a pointer variable, so
when we write ptr +i, then adding i gives a pointer that points i elements
further along an array than the original pointer.
Programming Tip:
An
error is generated if an attempt is made to change the address of the array.
Since
++ptr and ptr++ are both equivalent to ptr
+1, incrementing a pointer using the unary ++ operator, increments the
address it stores by the amount given by sizeof(type) where type is the data
type of the variable it points to (i.e., 2 for an integer). For example,
consider Figure 7.4.
If ptr originally points to arr [2], then ptr++ will point to the next element, i.e., arr [3]. This is shown in Figure 7.4.
Had
this been a character array, every byte in the memory would have been used to
store an individual character. ptr++ would then add only 1 byte to the address
of ptr.
When
using pointers, an expression like arr[i]
is equivalent to writing * (arr+i).
If arr is the array name, then the
compiler implicitly takes
arr = &arr [0]
To
print the value of the third element of the array, we can straightaway use the
expression * (arr+2). Note that
arr[i] = * (arr + i)
Many
beginners get confused by thinking of array name as a pointer. For example,
while we can write
ptr = arr; // ptr = &arr [0]
we
cannot write
arr = ptr;
This
is because while ptr is a variable, arr is a constant. The location at which
the first element of arr will be stored cannot be changed once has arr [] been declared. Therefore, an
array name is often known to be a constant pointer.
Programming Tip: When
an array is passed to a function, we are actually passing a pointer to the
function. Therefore, in the function declaration you must declare a pointer to
receive the array name.
To
summarize, the name of an a pointer to the array is equivalent to the address
of its first element, as a pointer is equivalent to the address of the element
that it points to. Therefore, arrays and pointers use the same concept.
Note
arr[i],
i[arr], *(arr+i), *(i+arr) gives the same value.
Look
at the following code and understand the result of Look at the following code
and understand executing them.
main()
{
int arr[]={1,2,3,4,5};
int *ptr, i;
ptr=&arr [2];
*ptr = -1;
* (ptr+1) = 0;
*(ptr-1) = 1;
printf("\n Array is: ");
for (i=0;i<5;i++)
printf("%d", * (arr+i);
}
Output
Array is: 1 1 -1 0 5
In
C we can add or subtract an integer from a pointer 2010 23 to get a new
pointer, pointing somewhere other than the original position. C also permits
addition and subtraction of two pointer variables. For example, look at the
code given below.
main()
{
int arr[]={1,2,3,4,5,6,7,8,9};
int *ptr1, *ptr2;
ptr1 = arr;
ptr2 = ptr+2;
printf("%d", ptr2 –ptr1);
}
Output
2
In
the code, ptr1 and ptr2 are pointers pointing to the
elements of the same array. We may subtract two pointers as long as they point
to the same array. Here, the output is2 because there are two elements between ptr1 and ptr2. Both the pointers must point to the same array or one past
the end of the array, otherwise this behaviour cannot be defined.
Moreover,
C also allows pointer variables to be com- pared with each other. Obviously, if
two pointers equal, then they point to the same location in the array. However,
if one pointer is less than the other, it means that the pointer points to some
element nearer to the beginning of the array.
17. Write a program
to display an array of given numbers.
#include <stdio.h>
main()
{
int arr[]={1,2,3,4,5,6,7,8,9};
int *ptrl, *ptr2;
ptr1 = arr;
ptr2 = &arr [8];
while (ptr1<=ptr2)
{
printf("%d", *ptr1);
ptr1++;
}
}
Output
1 2 3 4 5 6 7 8 9
18.
Write a program to read and display an array of n integers.
#include <stdio.h>
int main()
{
int i, n;
int arr [10], *parr = arr;
printf("\n_Enter the number of
elements: ");
scanf("%d", &n);
printf("\n Enter the elements:
");
for(i = 0; i < n; i++)
scanf("%d", parr+i);
printf("\n The elements
entered are: ");
for (i=0; i < n; i++)
printf("\t %d", *
(parr+i));
return 0;
}
Output
Enter the number of elements: 5
Enter the elements: 1 2 3 4 5
The elements entered are: 1 2 3 4 5
Note
An
object is a named region of storage; an Ivalue is an expression referring to an
object.
19. Write a program
to find mean of n numbers using arrays.
#include <stdio.h>
int main()
{
int i, n, arr [20], sum =0;
int *pn = &n, *parr = arr,
*psum = ∑
float mean = 0.0, *pmean =
&mean;
printf("\n Enter the number of
elements: ");
scanf("%d", pn);
for (i = 0; i < *pn; i++)
{
printf("\n Enter the number:
");
scanf("%d", (parr + i));
}
for (i=0; i < *pn; i++)
*psum += * (arr + i);
*pmean = (float) *psum / *pn;
printf("\n The numbers you
entered are: ");
for (i=0; i < *pn; i++)
printf("%d ", * (arr +
i));
printf("\n The sum is:
%d", *psum);
printf("\n The mean is:
%.2f", *pmean);
return 0;
}
Output
Enter the number of elements: 5
Enter the number: 1
Enter the number: 2
Enter the number: 3
Enter the number: 4
Enter the number: 5
20. Write a program
to find the largest of n numbers using arrays. Also display its position.
#include <stdio.h>
int main()
{
int i, n, arr [20], large = -32768,
pos = 0;
int *pn = &n, *parr = arr,
*plarge = &large, *ppos = &pos;
clrscr();
printf("\n Enter the number of
elements in the array: ");
scanf("%d", pn);
for (i = 0; i < *pn; i++)
{
printf("\n Enter the number:
");
scanf("%d", parr+i);
}
for (i = 0; i < *pn; i++)
{
if (* (parr+i) > *plarge)
{
*plarge = *(parr+i);
*ppos = i;
}
}
printf("\n The numbers you
entered are:") ;
for (i = 0; i < *pn; i++)
printf("%d ", *
(parr+i));
printf("\n The largest of
these numbers is: %d", *plarge);
printf("\n The position of the
largest number in the array is: %d", *ppos);
return 0;
}
Output
Enter the number of elements in the
array: 5
Enter the number: 1
Enter the number: 2
Enter the number: 3
Enter the number: 4
Enter the number: 5
The numbers you entered are:
1 2 3 4 5
The largest of these numbers is: 5
The position of the largest number
in the array is: 4
Programming in C: Unit III (b): Pointers : Tag: : Syntax with Example C Programs - Pointers and Arrays
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation