Dynamically Allocated Memory in Structure Array (in C) -
i having problem figuring out how dynamically allocate memory array structure. need utilize array of defined construction can iterate on print info. however, asked dynamically allocate memory array.
the problem when utilize malloc() in code (you see commented out) throws pointer off array index. there way accomplish dynamically allocating memory while pointing array indicies store data?
disclaimer: new c, if coding hurts eyes, sincerely apologize in advance. looking understand programme rather finish it.
program instructions:
write c programme uses 'struct' define construction containing next student:
information:
initial [character] age [integer] id [integer] grade [character]your programme should print above info 5 students.
hint: utilize array of structures , iterate on array print information.
create construction with:
variable info of type int a character pointer called tag. pointer should point address of string.check if memory available , dynamically allocate required memory structure.
assign values construction variable , print them console.
code far:
# include <stdio.h> # include <string.h> # include <stdlib.h> int main (void) { struct pupil { char initial [21]; int age; float id; char grade [3]; } list[5]; struct pupil * tag = list; //line below trying do; throws pointer off of struct array //tag = ( struct pupil * ) malloc ( sizeof ( struct pupil )); strcpy(tag->initial, "kj"); tag->age = 21; tag->id = 1.0; strcpy (tag->grade, "a"); tag++; strcpy(tag->initial, "mj"); tag->age = 55; tag->id = 1.1; strcpy (tag->grade, "b"); tag++; strcpy(tag->initial, "cj"); tag->age = 67; tag->id = 1.2; strcpy (tag->grade, "c"); tag++; strcpy(tag->initial, "sj"); tag->age = 24; tag->id = 1.3; strcpy (tag->grade, "d"); tag++; strcpy(tag->initial, "dj"); tag->age = 27; tag->id = 1.4; strcpy (tag->grade, "f"); tag++; int n; ( n = 0; n < 5; n++ ) { printf ( "%s %d, id %f, grade %s\n", list [ n ].initial, list [ n ].age, list [ n ].id, list [ n ].grade); } homecoming 0; }
try this.... here declaring 1 construction pointer tag
. allocating memory 5 pupil construction . after assigning values ,now tag
pointing lastly value.so decremented 5 times.this programme using 1 pointer. if want can seek using array of pointer.
i mentioned changes ,in program,as //changes
# include <stdio.h> # include <string.h> # include <stdlib.h> int main (void) { struct pupil { char initial [21]; int age; float id; char grade [3]; } list[5]; struct pupil * tag; tag = ( struct pupil * ) malloc (5* sizeof (struct student));//changes strcpy(tag->initial, "kj"); tag->age = 21; tag->id = 1.0; strcpy (tag->grade, "a"); tag++; strcpy(tag->initial, "mj"); tag->age = 55; tag->id = 1.1; strcpy (tag->grade, "b"); tag++; strcpy(tag->initial, "cj"); tag->age = 67; tag->id = 1.2; strcpy (tag->grade, "c"); tag++; strcpy(tag->initial, "sj"); tag->age = 24; tag->id = 1.3; strcpy (tag->grade, "d"); tag++; strcpy(tag->initial, "dj"); tag->age = 27; tag->id = 1.4; strcpy (tag->grade, "f"); tag++; int n; tag=tag-5;//changes ( n = 0; n < 5; n++ ) { printf ( "%s %d, id %f, grade %s\n", tag->initial, tag->age, tag->id, tag->grade); tag++;//changes } homecoming 0; }
using array of pinter...(instead of using separate assignment, can utilize loop assigning values every student)
#include <stdio.h> #include <string.h> #include <stdlib.h> int main (void) { struct pupil { char initial [21]; int age; float id; char grade [3]; } list[5]; struct pupil *tag[5]; int i; for(i=0;i<5;i++) tag[i]= ( struct pupil * ) malloc (sizeof (struct student)); strcpy(tag[0]->initial, "kj"); tag[0]->age = 21; tag[0]->id = 1.0; strcpy (tag[0]->grade, "a"); strcpy(tag[1]->initial, "mj"); tag[1]->age = 55; tag[1]->id = 1.1; strcpy (tag[1]->grade, "b"); strcpy(tag[2]->initial, "cj"); tag[2]->age = 67; tag[2]->id = 1.2; strcpy (tag[2]->grade, "c"); strcpy(tag[3]->initial, "sj"); tag[3]->age = 24; tag[3]->id = 1.3; strcpy (tag[3]->grade, "d"); strcpy(tag[4]->initial, "dj"); tag[4]->age = 27; tag[4]->id = 1.4; strcpy (tag[4]->grade, "f"); ( = 0; < 5; i++ ) { printf ( "%s %d, id %f, grade %s\n", tag[i]->initial, tag[i]->age, tag[i]->id, tag[i]->grade); } homecoming 0; }
c arrays pointers memory-management dynamic
No comments:
Post a Comment