Given an array from the user the task is to left shift array all elements by one place using pointers.
Example:
Consider an array of size 5.
| 2 | 6 | 4 | 9 | 7 |
Left shifted array.
| 6 | 4 | 9 | 7 | 2 |
Note: Here the first element is brought to last position of the array.
C
#include <stdio.h>
int main()
{
int n=0,temp=0;
printf("Enter the size of array: ");
scanf("%d",&n);
int ar[n];
int *p=ar;
printf("Enter the array elements:\n");
for(int i=0;i<n;i++)
{
scanf("%d",p);
p++;
}
p=ar;
temp=*p;
for(int i=0;i<n-1;i++)
{
*p=*(p+1);
p++;
}
*p=temp;
p=ar;
printf("Left shifted array: ");
for(int i=0;i<n;i++)
{
printf("%d ",*p);
p++;
}
return 0;
}C