Thread: Need a database table - array of structs?

  1. #1
    Registered User
    Join Date
    Aug 2010
    Beer Capital, USA

    Question Need a database table - array of structs?


    I'm super new to C and trying to write my first real program.

    I'm not connecting to an actual DB, I just need a table to hold records during runtime. The user will be adding and deleting records from the table, so I need to be able to change the number of elements in the array (i.e. adding and deleting records).

    I am currently using an array of structs but I don't know how to redefine the array larger (for adding records) or smaller (for deleting records). And in all reality, I don't even know if an array of structs is the right tool for this job.

    Here is how I'm creating the array of structs:

    // define the structure
    struct CueRecord
    	int PrimaryKey;
    	float CueNumber;
     	char CueName[64];
    // declare an array of the structure
    struct CueRecord CueSheet[10];
    Then I am able to access this bad boy with something like:

    // add a record to the cue sheet table 
    int AddCueToCueSheet(int ArrayIndex, int PrimaryKey, float CueNumber, char CueName[64])
    	// this is for testing purposes
    	printf("\nAdding record with array index %d\n", ArrayIndex);
    	// insert a record into table
    	CueSheet[ArrayIndex].PrimaryKey = PrimaryKey;
    	strcpy(CueSheet[ArrayIndex].CueName, CueName);
    	return 0;
    Is there a way to redefine the array from 10 elements to 9... or from 10 to 11 (etc)? Or should I be looking at a different tool for this job?

    I'm not looking for it to be spelled out for me... just looking to be pointed in the right direction. Thanks in advance!
    Last edited by PaulyWally; 08-09-2010 at 09:04 AM. Reason: typos

  2. #2
    Join Date
    Oct 2007
    Inside my computer
    malloc/free. But since you're using C, you're in for a tough time. As usual.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Something like a linked list is probably best suited for this, if you need it to be truly dynamic. In essence you add a pointer to the struct, pointing to the next struct etc. Then add functions to insert, delete new structs and so on.

    Here is a nice introductory paper on linked lists:

    If that feels like too soon, just make the array large enough.
    Last edited by Subsonics; 08-09-2010 at 08:39 AM.

  4. #4
    Registered User
    Join Date
    May 2010
    Of course, it is perfectly possible in C.
    11.3 Reallocating Memory Blocks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting the size of an array of structs
    By steve1_rm in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2008, 06:29 AM
  2. Sorting an Array of Structs not working
    By ashcan1979 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2006, 03:07 PM
  3. accessing array of structs values
    By WaterNut in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2004, 08:47 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM