Nope.
This however, would:
Code:
//Malloc exmaple
#include <stdio.h>
#include <stdlib.h>
int main (void){
int *studentAgeArray;
int numStudents;
printf("Please enter the number of students.\nEntry: \n");
scanf("%d", &numStudents);
// malloc wants us to tell it how many bytes we want.
// an int is sizeof(int) bytes, and we want numStudents
// integers, so we want sizeof(int) * numStudents bytes
// in total.
//
// In general, the idea is (myType*)malloc(sizeof(myType)*number_of_elements_I_want);
studentAgeArray=(int*)malloc(sizeof(int) * numStudents); // Actually, malloc() could fail and return 0
// There are ways to correct this. I have never actually seen malloc fail, but whatever:
// studentAgeArray=0;
// while(studentAgeArray == 0) studentAgeArray=(int*)malloc(sizeof(int) * numStudents);
//
// This would either hang in an infinite loop if we can't get memory, or it would eventually
// allocate our memory
// This proves we allocated memory. Feel free to omit. It was just for the example.
printf("studentAgeArray = %p\n", studentAgeArray);
// We have to free this dynamically allocated memory at some point
free(studentAgeArray);
system("PAUSE");
return 0;
}
A sample output is:
Code:
Please enter the number of students.
Entry:
4
studentAgeArray = 0x1244010