question about array of structs...

This is a discussion on question about array of structs... within the C Programming forums, part of the General Programming Boards category; if i have a struct like this... Code: typedef struct student_profile{ long long int stud_num; char name[50]; char bday[50]; char ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    39

    question about array of structs...

    if i have a struct like this...

    Code:
    typedef struct student_profile{
    	long long int stud_num;
    	char name[50];
    	char bday[50];
    	char course[50];
    	} STUDENT;
    then i have an array of the given struct above...

    Code:
    STUDENT array[20];
    how could i tell if the slot in the array is free or not [null or not]? [for finding free slots in the array & for stopping when displaying all the data in the array]...
    will this work?

    Code:
    for(i = 0; i < 20; i++)
    {
    	if(array[i].stud_num == NULL)
    	{
    		free = i;
    		break;
    	}
    }
    and then assign like "array[free].name, etc..."?
    thanks in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Quote Originally Posted by Huskar
    how could i tell if the slot in the array is free or not [null or not]?
    Use another variable to keep track of the number of elements in use. Make sure that the number of elements in use never exceeds the size of the array.

    By the way, avoid using free as a variable name. free is the name of a commonly used function in the C standard library, available by including <stdlib.h>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You could assign a zero to the student number, indicating that the record was unused.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Printing an array of structs?
    By blernblan in forum C Programming
    Replies: 4
    Last Post: 04-28-2009, 03:04 PM
  3. Array of Structs question
    By WaterNut in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2004, 02:58 PM
  4. array of structs initialization - PLZ help...
    By Vanya in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2002, 07:10 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21