Thread: populate an array struct

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    9

    populate an array struct

    i can populate a single struct and display it with no problem. But for some reason, when arrays get involved, I get so lost.

    i want to get data for a maximum of [30] of my students and store them in an array for me to print after collecting the data.

    i have done a program for one. works great. took me forever. I hava function ready to print the array.

    I just need to fill it now with struct data.

    help please... "point" me in the right direction.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    
    #define MAX_STUDENTS 30
    #define MAX_SIZE 48
    
    
    /*Type Descriptions*/
    struct student_data {
    	char lastName [MAX_SIZE];
    	char firstName [MAX_SIZE];
    	char majCode [5];
    	char yearInSchool [MAX_SIZE];
    	double crHrCompl;
    	double gPa;
    };
    
    /*fx prototypes*/
    int get_info(struct student_data  *studentPtr);
    void student_output (struct student_data students [MAX_STUDENTS], int how_many);
    int how_many = 0;
    
    
    int main( void )
    {
    	struct student_data students[MAX_STUDENTS];
    
    	how_many = get_info (students);
    
    	printf("student 1 name:%s\n",students[0].firstName);
    	printf("student 2 name:%s\n",students[1].firstName);
    
    
    	student_output (students, how_many) ;
    
    	return 0 ;
    }
    
    /*****fx******/
    
    int get_info(struct student_data  *studentPtr )
    {
    	char input_string [MAX_SIZE];
    	char goAgain = 'y';
    	int i = 0;
    	int count = 0;
    
    	do{
    
    	printf("Enter Last Name: ");
    		fgets(input_string,MAX_SIZE,stdin);
    
    		input_string[strlen(input_string-1)] = '\0';
    		
    		strncpy(studentPtr->lastName, input_string, MAX_SIZE);
    
    		studentPtr->lastName[MAX_SIZE];
    
    	printf("Enter First Name: ");
    		fgets(input_string,MAX_SIZE,stdin);
    
    		input_string[strlen(input_string-1)] = '\0';
    		
    		strncpy(studentPtr->firstName, input_string, MAX_SIZE);
    
    		studentPtr->firstName[MAX_SIZE];
    
    	printf("Enter Major Code: ");
    		fgets(input_string,MAX_SIZE,stdin);
    		
    		input_string[strlen(input_string-1)] = '\0';
    		
    		strncpy(studentPtr->majCode, input_string, 4);
    		
    		studentPtr->majCode[4] = '\0';
    
    	printf("Enter Year in School: ");
    		fgets(input_string,MAX_SIZE,stdin);
    
    		input_string[strlen(input_string-1)] = '\0';
    		
    		strncpy(studentPtr->yearInSchool, input_string, MAX_SIZE);
    
    		studentPtr->yearInSchool[MAX_SIZE];
    
    	printf("Enter Credit Hours: ");
    		fgets(input_string,MAX_SIZE,stdin);
    
    		studentPtr->crHrCompl = atof(input_string);
    
    	printf("Enter GPA: ");
    		fgets(input_string,MAX_SIZE,stdin);
    	
    		studentPtr->gPa = atof(input_string);
    		
    		i++;
    
    		printf("Press "y" to add another, otherwise press "n" to exit. \n");
    		
    	scanf("%c%*c",&goAgain);
    			if (goAgain != 'y') {
    				break;	
    			}
    	
    	}/*do end braklet*/
    
    		while (i != MAX_STUDENTS);
    
    		count = i;
    
    		return count;
    }
    
    
    void student_output (struct student_data students [MAX_STUDENTS], int how_many)
    {
    	int i = 0;
    
    	do{
    	for(i = 0; i < how_many; i++){
    	printf("Last Name     : %s", students[i].lastName);
    	printf("First Name    : %s", students[i].firstName);
    	printf("Major Code    : %s\n", students[i].majCode);
    	printf("Year in School: %s", students[i].yearInSchool);
    	printf("Credit Hours  : %4.2f\n", students[i].crHrCompl);
    	printf("GPA           : %4.2f\n", students[i].gPa);
    	
    	i++;
    	
    		}
    	}
    	while( i !=how_many);
    
    	return;
    }
    Last edited by flipguy_ph; 04-17-2009 at 01:17 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You could do a couple of things. One is to advance the pointer at the end of the loop:

    Code:
    studentPtr+=sizeof(struct student_data);
    Which the compiler promises to give you contiguous memory for your array, so that's okay.

    The other is to use a subscript to the array ([i]) like you do in student_output. Witness that with structs, this:
    Code:
    void function (struct somestruct *ptr) {
    *ptr could refer to a single struct, or an array. Since you have submitted an array to get_info, I believe you will have to add the subscript anyway just to get this to compile properly; the gcc error will be something like "request for member in something not a structure".

    Another suggestion would be to put your questions into a char array and use another loop inside the do...while like this
    Code:
    char questions[10][32]={"Enter name: ","Enter size: ", "Enter score: "};
    for (i=0;i<10;i++) {
         printf("%s",questions[i]);
    Use a temp variable with fgets, and then a case statement to put the data you get into the right part of the struct (if you used a set of parallel arrays instead of a struct, it would be even simpler; all you would need to do is atof the last two iterations).
    Last edited by MK27; 04-17-2009 at 02:03 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    A couple of things --- first you never increment 'i' at the end of the do-while loop. Also, you never initialize your studentPtr. The statement
    "strncpy(studentPtr->lastName, input_string, MAX_SIZE);"
    points to what student?

    Maybe something like "studentPtr = student_data &tempStudent"
    Then it has to assign a value, doesn't it? studentPtr-> lastName = "some char[] or char* value, for example.
    You never insert the student into an array!

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    awesome... thought so but wasn't sure where and how.

    I like the additional loop idea, i'll try that for my next programs.

    So i tried out the subscripts ... it feels good, but i think my loop is not working or i have the wrong loop in there. it looks right but my output is wrong.

    Code:
    /*Name of Programmer: [email protected]
      date written: 03125009
      Purpose: A programm that collects and keeps student information.
      Written for CSC_2473_290_31001
      Complied on Centrino Duo T2250 1.73GHz machine running MS Win XP and complied 
        with MS Visual C++ 2008 Express
    
    */
    
    #pragma warning(disable:4996)  
    /*that will stop the warning on scanf being deprecated */
    
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    
    #define MAX_STUDENTS 30
    #define MAX_SIZE 48
    
    
    /*Type Descriptions*/
    struct student_data {
    	char lastName [MAX_SIZE];
    	char firstName [MAX_SIZE];
    	char majCode [5];
    	char yearInSchool [MAX_SIZE];
    	double crHrCompl;
    	double gPa;
    };
    
    /*fx prototypes*/
    int get_info(struct student_data students [MAX_STUDENTS]);
    void student_output (struct student_data students [MAX_STUDENTS], int how_many);
    int how_many = 0;
    
    
    int main( void )
    {
    	struct student_data students[MAX_STUDENTS];
    
    	how_many = get_info (students);
    
    	/* printf("student 1 last name:%s\n",students[0].lastName);
    	printf("student 2 last name:%s\n",students[1].lastName);*/
    
    
    	student_output (students, how_many) ;
    
    	return 0 ;
    }
    
    /*****fx******/
    
    int get_info( struct student_data students[MAX_STUDENTS] )
    {
    	char input_string [MAX_SIZE];
    	char goAgain = 'y';
    	int i = 0;
    	int count = 0;
    
    
    
    	do{
    		
    
    	printf("Enter Last Name: ");
    		fgets(input_string,MAX_SIZE,stdin);
    
    		input_string[strlen(input_string-1)] = '\0';
    		
    		strncpy(students->lastName, input_string, MAX_SIZE);
    
    		students[i].lastName[MAX_SIZE];
    		
    
    	printf("Enter First Name: ");
    		fgets(input_string,MAX_SIZE,stdin);
    
    		input_string[strlen(input_string-1)] = '\0';
    		
    		strncpy(students->firstName, input_string, MAX_SIZE);
    
    		students[i].firstName[MAX_SIZE];
    
    	printf("Enter Major Code: ");
    		fgets(input_string,MAX_SIZE,stdin);
    		
    		input_string[strlen(input_string-1)] = '\0';
    		
    		strncpy(students->majCode, input_string, 4);
    		
    		students[i].majCode[4] = '\0';
    
    	printf("Enter Year in School: ");
    		fgets(input_string,MAX_SIZE,stdin);
    
    		input_string[strlen(input_string-1)] = '\0';
    		
    		strncpy(students->yearInSchool, input_string, MAX_SIZE);
    
    		students[i].yearInSchool[MAX_SIZE];
    
    	printf("Enter Credit Hours: ");
    		fgets(input_string,MAX_SIZE,stdin);
    
    		students[i].crHrCompl = atof(input_string);
    
    	printf("Enter GPA: ");
    		fgets(input_string,MAX_SIZE,stdin);
    	
    		students[i].gPa = atof(input_string);
    		
    		i++;
    
    		printf("Add another? Press 'y' to add another, otherwise press n to exit. \n");
    		
    	scanf("%c%*c",&goAgain);
    			if (goAgain != 'y') {
    				break;	
    			}
    		
    	}/*doend braklet*/
    	
    	while(i != MAX_STUDENTS);
    		
    
    		count = i;
    
    		return count;
    }
    
    
    void student_output (struct student_data students [MAX_STUDENTS], int how_many)
    {
    	int i = 0;
    
    	
    	for(i = 0; i < how_many; i++){
    	printf("Last Name     : %s", students[i].lastName);
    	printf("First Name    : %s", students[i].firstName);
    	printf("Major Code    : %s\n", students[i].majCode);
    	printf("Year in School: %s", students[i].yearInSchool);
    	printf("Credit Hours  : %4.2f\n", students[i].crHrCompl);
    	printf("GPA           : %4.2f\n", students[i].gPa);
    	
    	i++;
    	
    	if (i = how_many){
    		break;
    		}
    	}/*for end*/
    	
    	return;
    }
    i enter 2 sets of data for two students and
    for some reason i get only one set of data from the 2nd student entered, but with the 1st students gpa and credits.

    Last Name: students[1].lastName value
    First Name: students[1].firstName value
    Major Code: students[1].mjaCode value
    Year in school: students[1].yearInSchool value
    Credits completed: students[0].crHrCompl value <--- why?
    GPA : students[0].gPA value <------ why?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    	printf("Enter Last Name: ");
    		fgets(input_string,MAX_SIZE,stdin);
    
    		input_string[strlen(input_string-1)] = '\0';
    		
    		strncpy(students->lastName, input_string, MAX_SIZE);
    
    		students[i].lastName[MAX_SIZE];
    		
    
    	printf("Enter First Name: ");
    		fgets(input_string,MAX_SIZE,stdin);
    
    		input_string[strlen(input_string-1)] = '\0';
    		
    		strncpy(students->firstName, input_string, MAX_SIZE);
    
    		students[i].firstName[MAX_SIZE];
    
    	printf("Enter Year in School: ");
    		fgets(input_string,MAX_SIZE,stdin);
    
    		input_string[strlen(input_string-1)] = '\0';
    		
    		strncpy(students->yearInSchool, input_string, MAX_SIZE);
    
    		students[i].yearInSchool[MAX_SIZE];
    
    What are those lines supposed to be doing?


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    well... looks like nothing. it's been a long day.

    i was trying to implement what MK27 mentioned:
    The other is to use a subscript to the array ([i]) like you do in student_output. Witness that with structs, this:
    Code:
    void function (struct somestruct *ptr) {
    *ptr could refer to a single struct, or an array. Since you have submitted an array to get_info, I believe you will have to add the subscript anyway just to get this to compile properly; the gcc error will be something like "request for member in something not a structure".
    apparently.... i didn't do it right.
    my orginal idea which worked great for a single struct was:
    Code:
    int get_info(struct student_data  *studentPtr )
    {
    	char input_string [MAX_SIZE];
    	char goAgain = 'y';
    	int i = 0;
    	int count = 0;
    
    	do{
    
    	printf("Enter Last Name: ");
    		fgets(input_string,MAX_SIZE,stdin);
    
    		input_string[strlen(input_string-1)] = '\0';
    		
    		strncpy(studentPtr->lastName, input_string, MAX_SIZE);
    
    		studentPtr->lastName[MAX_SIZE];
    
    ect....ect...
    Now im trying to fill an array of student data rather than just one student data.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    What's this in your for loop
    Code:
    i++;
    	
    	if (i = how_many){
    		break;
    		}
    	}/*for end*/
    The last part is redundant, since the condition was i<how_many, but i++ means that i will get incremented twice in the loop.

    Here is a basic "for"
    Code:
    for (i=0;i<10;i++) {
         printf("%d\n",i);
    }
    Which will print 0-9 and then stop.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The subscripting should be here:
    Code:
    strncpy(students[i]->firstName, input_string, MAX_SIZE);
    Those other lines you added are meaningless.

    Also, since you changed the prototype:
    Code:
    int get_info(struct student_data  *studentPtr )
    int get_info( struct student_data students[MAX_STUDENTS] )
    You won't be able to use indirect notation (->); so you might as well change it back (as I said, with structs, *studentPtr could be a single struct or an array of structs, you don't need students[MAX]). The only place the subscripting is relevant is the red bit, above.
    Last edited by MK27; 04-17-2009 at 03:01 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    great!

    this is where i was when i began. I tried to do just that but kept getting errors. I restored the original code for the day and implemented your suggestion of where to apply the subscript.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    
    #define MAX_STUDENTS 30
    #define MAX_SIZE 48
    
    
    /*Type Descriptions*/
    struct student_data {
    	char lastName [MAX_SIZE];
    	char firstName [MAX_SIZE];
    	char majCode [5];
    	char yearInSchool [MAX_SIZE];
    	double crHrCompl;
    	double gPa;
    };
    
    /*fx prototypes*/
    int get_info(struct student_data  *studentPtr);
    void student_output (struct student_data students [MAX_STUDENTS], int how_many);
    int how_many = 0;
    
    
    int main( void )
    {
    	struct student_data students[MAX_STUDENTS];
    
    	how_many = get_info (students);
    
    	printf("student 1 name:%s\n",students[0].firstName);
    	printf("student 2 name:%s\n",students[1].firstName);
    
    
    	student_output (students, how_many) ;
    
    	return 0 ;
    }
    
    /*****fx******/
    
    int get_info(struct student_data  *studentPtr )
    {
    	char input_string [MAX_SIZE];
    	char goAgain = 'y';
    	int i = 0;
    	int count = 0;
    
    	do{
    
    	printf("Enter Last Name: ");
    		fgets(input_string,MAX_SIZE,stdin);
    
    		input_string[strlen(input_string-1)] = '\0';
    		
    		strncpy(studentPtr[i]->lastName, input_string, MAX_SIZE);
    
    		studentPtr->lastName[MAX_SIZE];
    
    	printf("Enter First Name: ");
    		fgets(input_string,MAX_SIZE,stdin);
    
    		input_string[strlen(input_string-1)] = '\0';
    		
    		strncpy(studentPtr->firstName, input_string, MAX_SIZE);
    
    		studentPtr->firstName[MAX_SIZE];
    
    	printf("Enter Major Code: ");
    		fgets(input_string,MAX_SIZE,stdin);
    		
    		input_string[strlen(input_string-1)] = '\0';
    		
    		strncpy(studentPtr->majCode, input_string, 4);
    		
    		studentPtr->majCode[4] = '\0';
    
    	printf("Enter Year in School: ");
    		fgets(input_string,MAX_SIZE,stdin);
    
    		input_string[strlen(input_string-1)] = '\0';
    		
    		strncpy(studentPtr->yearInSchool, input_string, MAX_SIZE);
    
    		studentPtr->yearInSchool[MAX_SIZE];
    
    	printf("Enter Credit Hours: ");
    		fgets(input_string,MAX_SIZE,stdin);
    
    		studentPtr->crHrCompl = atof(input_string);
    
    	printf("Enter GPA: ");
    		fgets(input_string,MAX_SIZE,stdin);
    	
    		studentPtr->gPa = atof(input_string);
    		
    		i++;
    
    		printf("Press Enter to add another, otherwise press x to exit. \n");
    		
    	scanf("%c%*c",&goAgain);
    			if (goAgain != 'y') {
    				break;	
    			}
    	
    	}/*do end braklet*/
    
    		while (i != MAX_STUDENTS);
    
    		count = i;
    
    		return count;
    }
    
    
    void student_output (struct student_data students [MAX_STUDENTS], int how_many)
    {
    	int i = 0;
    
    	do{
    	for(i = 0; i < how_many; i++){
    	printf("Last Name     : %s", students[i].lastName);
    	printf("First Name    : %s", students[i].firstName);
    	printf("Major Code    : %s\n", students[i].majCode);
    	printf("Year in School: %s", students[i].yearInSchool);
    	printf("Credit Hours  : %4.2f\n", students[i].crHrCompl);
    	printf("GPA           : %4.2f\n", students[i].gPa);
    	
    	
    		}
    	}
    	while( i !=how_many);
    
    	return;
    }
    ive been gettin these errors:

    Code:
    error C2232: '->lastName' : left operand has 'struct' type, use '.'
    warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int'
    warning C4024: 'strncpy' : different types for formal and actual parameter 2
    error C2198: 'strncpy' : too few arguments for call
    ...getting burned out....phew...

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by flipguy_ph View Post
    ...getting burned out....phew...
    Wow, I thought this would have been obvious but on the other hand, I did say "just there". You need to use the subscript every time you reference studentPtr. It is EXACTLY like what you do in student_output. Remember, studentPtr points to the entire array.

    And I made a sort of mistake; you can't use the indirect notation anyway (don't ask me why, it seems counter-intuitive, but someone may be along to explain why); so change all those -> to dots, meaning again it is the same. So either style of prototype would amount to the same thing.

    The indirect notation is for this:
    Code:
    struct this;
    func(&this);
    void func (struct *ptr) {
              ptr->element;
    }
    Which is passing the address of a single struct. But you are not doing that, you're passing a pointer to an array. So the syntax is the same as with chars, notice. (& with a single char, but the function prototype is the same).
    Last edited by MK27; 04-17-2009 at 03:50 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    you've been a great help MK27.

    thanks!

    i knew what you meant about "just there" not meaning "just" there. It's been a long day for a simple task. A lot of it has to do with me being at home with a 3 year old kid. my focus isn't focused.

    It all works out great. and i learned some new techniques too.

    cheers!

    time to cook dinner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. array of pointers of a struct
    By paulur in forum C Programming
    Replies: 18
    Last Post: 04-14-2006, 07:17 AM
  4. Writing an array of struct to file
    By stellastarr in forum C Programming
    Replies: 10
    Last Post: 03-25-2006, 06:59 PM
  5. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM