i defined a structure for a student
typedef struct {
char firstName[20];
char lastName[20];
int id;
char *birthDate; } Student; the function definition for a student is this Student createStudent(const char first_Name, const char last_Name, int ID, const char
birthDate_str) {
/* Declare a structure */
Student *b = ((Student *)malloc(sizeof(Student)*1));
strcpy(b->firstName, first_Name);
strcpy(b->lastName, last_Name);
strcpy(b->birthDate, birthDate_str);
b->birthDate = (char *) malloc(sizeof(char) * strlen(birthDate_str)+1); strcpy(b->birthDate, birthDate_str); b->id = ID; return b; } i need to use a for loop to print each student that i created Student s3 = createStudent("Jamie", "Peters", 1445562, "07/11/1988"); Student s4= createStudent("Chris", "Johnson", 340672, "05/10/1960"); Student s5 = createStudent("Belle", "Peters", 685590, "01/25/1998"); Student s6 = createStudent("Liz", "Erickson", 6792390, "11/15/2005"); Student s7 = *createStudent("Halle", "Smith", 123456, "02/14/2014");`

i have a student array to store each student's information
char *studentArray[6];
I want to use a for loop so that when the studentArray is at position 1 for example it would print information for student 3 (s3) and so on for each position. it should print like this
printf( "First Name : %s\n", student->firstName);
printf( "Last Name : %s\n", student->lastName);
printf( "Birthday : %s\n", student->birthDate);
printf( "ID : %d\n", student->id);
I cant figure out how to print using a for loop any help would be apreaciated.

Recommended Answers

All 2 Replies

Your student array would not be a char pointer array.
Student is now a new type.

If you must keep your structure containing a pointer, you're almost there.

Your createStudent function should return a pointer, and also needs to pass pointers insted of just char so the signature would be:

Student*  createStudent(const char*  first_Name, const char* last_Name, int ID, const char* birthDate_str)

Inside that function, you are attempting to copy a value into birthday before the malloc, so the program will crash. Keep in mind, you have that same strcpy done TWICE.

If you set a const or a #define for the number of recorsds you're using, you can make your loop count up to that number -- otherwise, you will need to calculate the relative size of the number of students created.

You can put all of your static pointers in an array of Student*.

Student* p_arrStudent[NUM_RECS] =
   {
      createStudent("Jamie", "Peters", 1445562, "07/11/1988"),
      createStudent("Chris", "Johnson", 340672, "05/10/1960"),
      /*...*/
   };

Since your string won't be that long, you can print the record with a single printf
using the format "First Name : %s\r\nLast Name : %s\r\nBirthday : %s\r\nID : %d\r\n"

The for-loop being something like: for (i = 0; i < NUM_RECS; i++)

The only thing left after that is cleaning-up (with free()) the memory of the elenents in the structures created.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.