Create C program that takes 1 parameter: a number.
Using that number, dynamically allocate a memory so you store that number of integers.
Write integers in order starting from 1 until you fill all that memory.
Print the addresses and values of the first and the last integer stored in the memory.
#include<stdio.h>
void fun(int n){
int *ptr = (int*) malloc(n * sizeof(int));
for(int i=0;i<n;i++){
ptr[i]=i+1;
}
printf("First ->\nAddress: %p \n value: %d\n",&ptr[0],ptr[0]);
printf("Last ->\nAddress: %p \n value: %d",&ptr[n-1],ptr[n-1]);
}
int main() {
fun(10);
}
Refer below for code indentation and output preview.
Get Answers For Free
Most questions answered within 1 hours.