Thread: question about array of structs...

  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
    28,413
    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>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 08:10 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM