Exercise 3
Consider the following array of integers:
Array of integers, named ‘arr’
|
|||||||||
2
|
6
|
-4
|
8
|
10
|
-12
|
14
|
16
|
18
|
20
|
num[0]
|
num[1]
|
num[2]
|
num[3]
|
num[4]
|
num[5]
|
num[6]
|
num[7]
|
num[8]
|
num[9]
|
Implement the following pointer
notations to traverse and display the given array ‘arr’.
1.
Printing array using *ptr
notation.
2.
Printing array using
array[i] notation.
3.
Printing array using
ptr[i] notation.
4.
Printing array using
*(array+i) notation.
5.
Printing array using
*(array+i) notation.
6.
Printing array using
*(ptr+i) notation.
SOURCE CODE:
#include<stdio.h>
void header()
{
printf("\n\n\n\t\tBuilt & Designed by Arslan Malik\n\n\t\t www.CWorldbyAS.blogspot.com\n\n\n");
}
int main()
{
int a[10]={2,6,-4,8,10,-12,14,16,18,20};
int *ptr;
int i=0;
ptr=a;
header();
system("pause");
system("cls");
printf("\n\n\t\tArray using *ptr Notation\n\n\n");
printf("\t");
for(i=0;i<10;i++)
{
printf(" %d ",*ptr);
*ptr++;
}
printf("\n\n\n");
system("pause");
system("cls");
printf("\n\n\t\tArray using Array[i] Notation\n\n\n");
printf("\t");
for(i=0;i<10;i++)
{
printf(" %d ",a[i]);
}
printf("\n\n\n");
system("pause");
system("cls");
printf("\n\n\t\tArray using ptr[i] Notation\n\n\n");
printf("\t");
ptr=a;
for(i=0;i<10;i++)
{
printf(" %d ",ptr[i]);
}
printf("\n\n\n");
system("pause");
system("cls");
printf("\n\n\t\tArray using *(array+i) Notation\n\n\n");
printf("\t");
for(i=0;i<10;i++)
{
printf(" %d ",*(a+i));
}
printf("\n\n\n");
system("pause");
system("cls");
printf("\n\n\t\tArray using *(ptr+i) Notation\n\n\n");
printf("\t");
for(i=0;i<10;i++)
{
printf(" %d ",*(ptr+i));
}
printf("\n\n\n");
system("pause");
system("cls");
printf("\n\tPress Enter to get out of Program . . . .");
printf("\n\n\n");
}
No comments:
Post a Comment