After learning about Pointers and types of pointers, It is very important to know about the relationship that exists between pointers and arrays in C Programming. Arrays being a sequential linear Data structure, uses memory in a very efficient way and along with the knowledge of pointers, manipulating arrays becomes even more easier and fun. This tutorial focuses on How to use pointers with arrays in C/C++.
Pointers with Arrays
When we declare an array, the compiler itself makes an arrangement for some memory required to save the elements of the array. This is called Allocation of memory. The address of the first element of the array is called the Base Address. This Base Address can be used as the address of the whole array, as if we increment the address (*p++), we can get the address of the next location, where the next element is stored as an in array ,data is stored in contiguous memory locations.
int arr[5]={1,2,3,4,5};
int *ptr=arr; //base addr = 1000
ptr++;
printf("%d\t",*ptr);
OUTPUT:
2
* This is because ptr++ operation increments the address that is being pointed by ptr by 1, since in this case data type is integer which has 4 bytes size, so next location will be 1004, which is the address of arr[1].
* arr = arr[0] = *(arr+0)
similarly, we can write arr[i] = *(arr+i)
* We can add or subtract an integer from a pointer to get a new pointer ,pointing somewhere else other than the original position . Also addition of 2 pointers in possible in C . For example ;
main()
{
int arr[]={10,20,30,4,5,6,7};
int *p1 , *p2 ;
p1=arr;
p2=arr+2;
printf("%d", p2-p1);
}
OUTPUT
2
Output is 2 because there are 2 elements between p1 and p2 in the array.
OUTPUT
Enter size of array
5
Enter elements
1 2 3 4 5
Elements entered by you are :
1 2 3 4 5
Ques 2. Write a program to find the mean of n numbers in an array.
int arr[5] = { 10 , 20 , 30 , 40 , 50 } ;
Here arr or arr[0] , will give us the base address. Being the name of the array , it also works as a pointer pointing to the first element of the array,
int arr[5]={1,2,3,4,5};
int *ptr=arr; //base addr = 1000
ptr++;
printf("%d\t",*ptr);
OUTPUT:
2
* This is because ptr++ operation increments the address that is being pointed by ptr by 1, since in this case data type is integer which has 4 bytes size, so next location will be 1004, which is the address of arr[1].
* arr = arr[0] = *(arr+0)
similarly, we can write arr[i] = *(arr+i)
* We can add or subtract an integer from a pointer to get a new pointer ,pointing somewhere else other than the original position . Also addition of 2 pointers in possible in C . For example ;
main()
{
int arr[]={10,20,30,4,5,6,7};
int *p1 , *p2 ;
p1=arr;
p2=arr+2;
printf("%d", p2-p1);
}
OUTPUT
2
Output is 2 because there are 2 elements between p1 and p2 in the array.
******************************
Ques 1. Write a program to read and display an array of n integers.
#include<stdio.h>
int main()
{
int i , n;
printf("Enter size of array\n");
scanf("%d",&n);
int arr[n] , *ptr = arr;
printf("Enter elements\n");
for ( i = 0 ; i < n ; i++ )
scanf("%d", ptr+1 );
printf("Elements entered by you are : \n");
for ( i =0 ; i < n ; i++ )
printf( "%d\t " , *(ptr + i) );
return 0;
}
OUTPUT
Enter size of array
5
Enter elements
1 2 3 4 5
Elements entered by you are :
1 2 3 4 5
Ques 2. Write a program to find the mean of n numbers in an array.
#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("Enter size of array\n");
scanf("%d", pn);
for ( i=0 ; i<*pn ; i++ )
{
printf("Enter the number : ");
scanf("%d", (parr+i) );
}
for ( i=0 ; i<*pn ; i++ )
*psum += *(arr+i);
*pmean = *psum / *pn ;
printf("The numbers you entered are : \n");
for ( i=0 ; i<*pn ; i++ )
printf("%d ", *(arr+i));
printf("\n The sum is : %d",*psum);
printf("\n The mean is : %d",*pmean);
return 0;
}
OUTPUT
Enter size of array
4
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 sum is : 15
The mean is : 3.00