-
Notifications
You must be signed in to change notification settings - Fork 0
/
Malloc.c
executable file
·49 lines (37 loc) · 1.23 KB
/
Malloc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
============================================================================
Name : Malloc.c
Author : May Moftah
Version : 1.0.0
Description : The following program prompts the user to enter the desired number of bytes. Memory is then allocated based on user input, using the malloc() function. Bytes allocated are displayed on the console with 10 elements per row.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
//function main begins program execution
int main () {
int n; //number of bytes
int i;
int j;
int *ptr; //pointer for memory allocation
printf ("Enter the number of bytes to dynamically allocate: \n" );
scanf ("%d", &n);
int a[n]; //array of bytes
ptr = (int*)malloc(n*sizeof(int)); //allocate memory based on user input
printf ("Numbers in dynamically allocated memory: \n");
for (i = 0; i < n; i++) {
//for each element, print out actual index
a[i] = i + 0;
}
for (j = 0; j < n; j++ ) {
//if the element is divisible by 10, begin a new output line
if (j%10 == 0) {
printf ("\n%6d", j);
}
//else print within the same line
else {
printf ("%6d", j);
}
}
printf ("\n");
} //end function main