Question: In c language Problem 1: Malloc and Realloc. Your job today is to create a program that takes in an arbitrary amount of data and
In c language
Problem 1: Malloc and Realloc. Your job today is to create a program that takes in an arbitrary amount of data and stores it into a correctly sized array. You will do this for 4 data types
Interger Double Character String
The user should be allowed to enter an infinite number of any of these data types. Your program should identify what data type was received, and then store it in the appropriate array. Make sure you dynamically allocate enough space for the array. The contents of all four arrays should be displayed after each input.
Example: Enter your input: 123
String list: Integer list: 123 Double List: Character list:
Enter your input: 7.5
String list: Integer list: 123 Double List: 7.5 Character list:
Enter your input: s
String list: Integer list: 123 Double List: 7.5 Character list: s
Enter your input: l.0.0
String list: 1.0.0 Integer list: 123 Double List: 7.5 Character list: s
Enter your input: Malloc is fun
String list: 1.0.0 Malloc is fun Integer list: 123 Double List: 7.5 Character list: s
Hints:
Use Integers to keep track of how many elements of each type you have (to use with realloc)
Before using realloc, you MUST malloc (even if you just malloc a size of 0)
Strings are arrays of characters(you will need to malloc a 2d array)
Break this problem into smaller parts tackle them one at a time.
follow the below codes:
void main(){
//declare as null to avoid issues
int *var = NULL;
int * array = calloc(2,sizeof(int));
//malloc for size of data type * int
var = malloc(sizeof(int));
var[0] = 10;
//realloc for new elements
var = realloc(var,sizeof(int)*2);
var[1] = 12;
func(&var);
free(var);
return;
void func(int **mem){
*mem = realloc(*mem,sizeof(int)*2);
(*mem)[2] = 13;
printf("%d ",);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
