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.
HERE IS THE CODE AS PER YOUR REQUIREMENT
CODE:
#include<stdio.h>
#include<stdlib.h>
int main() {
int n,i;
// pointer to hold base address of the block to be created
int* ptr;
// user input for a number parameter
printf("Enter the number of elements: ");
scanf("%d",&n);
// malloc method to dynamically allocate the memory
ptr = (int*)malloc(n * sizeof(int));
if(ptr != NULL) {
// Entering the elements from 1 to n into the memory
locations
printf("Memory allocated successfully using malloc()
method");
for (int i = 0; i < n; i++) {
ptr[i] = i+1;
}
// printing the values and assigned addresses of 1st and last
integer stored
printf("\n%d %p",ptr[0],&ptr[0]);
printf("\n%d %p",ptr[n-1],&ptr[n-1]);
}
else {
printf("Memory not allocated");
}
}
SCREENSHOTS:
OUTPUT:
IF YOU HAVE ANY QUERIES FEEL FREE TO ASK AT COMMENTS
PLEASE DO LIKE :)
Get Answers For Free
Most questions answered within 1 hours.