Would this be the proper way to extend a structure array?
typedef struct { int x,y,z;} student_record;
int main(){
student_record data_record[30]; // create array of 30 student_records
num_of_new_records = 5;
data_record = realloc(data_record,(sizeof(data_record) + (sizeof(student_record)*num_of_new_records)));
// do I now have an array of 35 student_records???
No – you can’t assign to an array. Your code won’t even compile – did you try it?
If you want to
realloc()you need to have usedmalloc()(or one of its relatives):You probably shouldn’t be assigning the return value of
realloc()back to the original variable, either. If it fails for some reason, you’ll lose the original pointer and leak that memory.