180k views
5 votes
Write two statements that each use malloc to allocate an int location for each pointer. Sample output for given program:

User Skyy
by
5.1k points

1 Answer

3 votes

Answer:

In the following code, you can see the usage of malloc.

#include <stdio.h>

#include <stdlib.h>

int main(void) {

int* numberPointer1 = NULL;

int* numberPointer2 = NULL;

numberPointer1 = (int *)malloc(sizeof(int *));

numberPointer2 = (int *)malloc(sizeof(int *));

*numberPointer1 = 44;

*numberPointer2 = 99;

printf("numberPointer1 = %d, numberPointer2 = %d\\", *numberPointer1, *numberPointer2);

free(numberPointer1);

free(numberPointer2);

return 0;

}

Step-by-step explanation:

Malloc allocates memory in the heap according to the input size and returns a pointer to the location where it was allocated, with an extra, it clears all allocated space.

User Chris Sedlmayr
by
5.5k points