A struct is an assembled object that contains variables, pointers, or further structs. Members of a struct are individually accessed using the struct name, followed by a '.' and the member name. Example:
typedef struct { int x; int y; string name; } SPOT; // defines a struct type named "SPOT" ... SPOT myspot; // creates an uninitalized SPOT struct named "myspot"
SPOT* pspot; // creates an uninitalized pointer to a SPOT struct ... myspot.x = 10; myspot.name = "test!";
A struct can contain pointers to previously defined structs, and even pointers on itself, but no pointers to later defined structs:
typedef struct SPOT { int x; int y; string name; struct SPOT* next; // pointer to a SPOT struct } SPOT; // defines a struct type named "SPOT"
In lite-C, struct pointers can be initialized to a static struct. Example:
SPOT* myspot = { x = 1; y = 2; name = "my struct"; } // creates a new SPOT struct with initial values that is then referenced through the myspot pointer
Working with structs is explained in any C/C++ book, so we won't cover it here in detail. For creating structs or arrays of structs at run time, the standard C library functions malloc and free can be used. For initializing or copying structs, use the C library functions memset() and memcpy():
function foo() { SPOT myspot; memset(myspot,0,sizeof(myspot)); // set the struct content to zero (it's undefined after malloc) myspot.x = 1; SPOT* spot_array = malloc(100*sizeof(myspot)); // creates an array of 100 SPOT structs memcpy(&spot_array[0],myspot,sizeof(myspot)); // copy the myspot struct to the first member of the array ... free(myspot_array); // removes the created structs }
!! There is plenty information about C, C++, or Windows library functions on the Internet or in online C courses. It is highly recommended for advanced lite-C programming to work through such a course or book and learn about malloc, memcpy and all the other library functions that you can use.
#define zero(struct) memset((void*)&struct,0,sizeof(struct)) ... SPOT speed; zero(speed); // initializes the SPOT struct "speed" to zero
!! Unlike C/C++, sizeof(some struct) requires that at least one instance of the struct exists. Otherwise sizeof will return 0.
!! Arrays are internally treated as a pointer to a memory area. So, sizeof(some array) and sizeof(some pointer) always evaluates to 4 because that is the size of a pointer.
► latest version online