Você está na página 1de 2

9/23/2019 https://mvgrce.codetantra.com/secure/labs-q.jsp?

sNo=49&qId=59dca4c20cf2e6552052c3ae&bd=AY3RFZHVEQg%3D%3D&lid=5c2…

S.No: 49 Exp. Name: Write a C program to access elements of an Array using Pointers Date:

Aim:

Page No:
Write a program to read and display the elements of an array using pointers.

At the time of execution, the program should print the message on the console as:

ID: 18331a0431
Enter n value :

For example, if the user gives the input as:

Enter n value : 4

Next, the program should print the message on the console as:

Enter 4 values :

For example, if the user gives the input as:

Enter 4 values : 11 55 44 33

then the program should print the result as:

The given array elements are : 11 55 44 33

Note: Write the functions read() and display() in AccessArray.c .


Source Code:

AccessArrayUsingPointers.c

#include <stdio.h>
#include <stdlib.h>
#include "AccessArray.c"
void main() {
int *p, n, i;
printf("Enter n value : ");
scanf("%d", &n);
p = (int *) malloc(n * sizeof(int));
printf("Enter %d values : ", n);
read(p, n);
printf("The given array elements are : ");
display(p, n);
printf("\n");
}
MVGR College of Engineering (Autonomous)

AccessArray.c

void read(int *p,int n)


{
int i;
for(i=0;i<n;i++)
{
scanf("%d",(p+i));
}
}
void display(int *p,int n)

https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=49&qId=59dca4c20cf2e6552052c3ae&bd=AY3RFZHVEQg%3D%3D&lid=5c2344c364bac… 1/2
9/23/2019 https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=49&qId=59dca4c20cf2e6552052c3ae&bd=AY3RFZHVEQg%3D%3D&lid=5c2…
{
int i;
for(i=0;i<n;i++)
{

Page No:
printf("%d ",*(p+i));
}
}

ID: 18331a0431
Execution Results - All test cases have succeeded!

Test Case - 1

User Output
Enter n value : 5
Enter 5 values : 2 5 7 4 1
The given array elements are : 2 5 7 4 1

MVGR College of Engineering (Autonomous)

https://mvgrce.codetantra.com/secure/labs-q.jsp?sNo=49&qId=59dca4c20cf2e6552052c3ae&bd=AY3RFZHVEQg%3D%3D&lid=5c2344c364bac… 2/2

Você também pode gostar