#include #include #include // Let's define a constant #define SIZE 20 // Let's define a structure with three members (Method 1) //struct s1 { // long ts; // char location[SIZE]; // int temp; //}; //typedef struct s1 Reading; // Shorthand, common use (Method 2) // This syntax is more commonly used than the one above typedef struct s2 { long ts; char location[SIZE]; int temp; } Reading; // -------------------------------------------------- int main(int argc, char* argv[]) { // Let's declare variables of type structure //struct s1 item = {10, "XXAAACC", 74}; //struct s1 array[2]; //struct s1 *pointer; // Or use the follows statements (you need the typedef above) Reading item = {10, "XXAAACC", 74}; Reading array[2]; Reading *pointer; // Operating on a variable of type struct sensor_reading printf("Initial sample is (%ld,%s,%d)\n",item.ts,item.location,item.temp); long time = 20; char *location = "EE037"; int temp = 67; item.ts = time; strncpy(item.location,location,SIZE); item.temp = temp; printf("Updated sample is (%ld,%s,%d)\n",item.ts,item.location,item.temp); // Operating on arrays of structures array[0].ts = time; array[0].temp = temp; strncpy(array[0].location,location,SIZE); printf("Element in array is (%ld,%s,%d)\n", array[0].ts, array[0].location, array[0].temp); // Using a pointer to a structure pointer = &item; (*pointer).ts = 30; printf("Sample is (%ld,%s,%d)\n",(*pointer).ts, (*pointer).location, (*pointer).temp); // The syntax below is more commonly used pointer->ts = 40; printf("Sample is (%ld,%s,%d)\n",pointer->ts, pointer->location, pointer->temp); return 0; }