Structs

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

In standard C / C++, members of structs are accessed by a dot '.' and members of struct pointers are accessed by an arrow '->'. In lite-C, the dot '.' can normally be used for both because the compiler automatically detects whether a simple object is a pointer or not. You only need to give the '->' in ambiguous cases, for instance when the struct pointer is a member of another struct pointer.

SPOT* myspot = { x = 1; y = 2; name = "my struct"; }
...
myspot->x = 1; // standard C/C++; works also in lite-c
myspot.y = 2;  // lite-C 

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 = malloc(sizeof(SPOT)); // creates a new SPOT struct at runtime
  memset(myspot,0,sizeof(SPOT)); // set the struct content to zero (it's undefined after malloc)
  myspot->x = 1;  
  SPOT* spot_array = malloc(100*sizeof(SPOT)); // creates an array of 100 SPOT structs
  memcpy(&spot_array[0],myspot,sizeof(SPOT));  // copy the myspot struct to the first member of the array
  ...
  free(myspot); // removes the created structs
  free(myspot_array);
}

 !!  There is plenty information about C, C++, or Windows library functions on the Internet or in online C courses. It is highly recommended for adcanced 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.

sizeof(struct)

The sizeof() macro that is used in the example above gives the size of a variable or a struct in bytes. This can be used to initialize structs:
#define zero(struct) memset((void*)&struct,0,sizeof(struct))
...
SPOT speed;
zero(speed);	// initializes the SPOT struct "speed" to zero

 !!  Arrays are internally treated as a pointer to a memory area. So, sizeof(any_array) and sizeof(any_pointer) always equals to 4 because that is the size of a pointer.

See also:

Variables, pointers, functions

► latest version online