I am new to C. I am trying to get an understanding of the malloc function for creating variables while the program is running. The program I wrote below, I don't know if it works or not, is the new variable (array in this case)being created?
#include <stdio.h>
void create_array() {
int i;
int *ptr=(int)malloc(sizeof(int)*50);
printf("Array being created any initialized...\n"
for(i = 0; i < 51; ++i) {
ptr = i;
printf("%d\n", i);
}
}
int main() {
int create;
printf("Would you like to create an array of type int with 50 elements?\n1 - Yes\t0 - No: "
scanf("%d", &create);
if(create) {
create_array();
}
}
shaun
#include <stdio.h>
void create_array() {
int i;
int *ptr=(int)malloc(sizeof(int)*50);
printf("Array being created any initialized...\n"
for(i = 0; i < 51; ++i) {
ptr = i;
printf("%d\n", i);
}
}
int main() {
int create;
printf("Would you like to create an array of type int with 50 elements?\n1 - Yes\t0 - No: "
scanf("%d", &create);
if(create) {
create_array();
}
}
shaun