C programming/Structures
From Teknologisk videncenter
Basic structrues
Example 1
Create and instantiate a structure
#include <stdio.h>
#include <stdlib.h>
struct coor {
int x;
int y;
};
int main( void ) {
struct coor place;
place.x = 17;
place.y = 19;
printf("Tallet er %i\n",place.x+place.y);
return(0);
}
Example 2
Same as example 1, but using as typedef
#include <stdio.h>
#include <stdlib.h>
struct coor {
int x;
int y;
};
typedef struct coor coordinate;
int main( void ) {
coordinate place;
place.x = 17;
place.y = 19;
printf("Tallet er %i\n",place.x+place.y);
return(0);
}
Example 3
In example 3 the memory is reserver with malloc and indirect addressing is used to access the structure.
#include <stdio.h>
#include <stdlib.h>
struct coor {
int x;
int y;
};
typedef struct coor coordinate;
int main( void ) {
coordinate *place = malloc( sizeof( coordinate ) );
(*place).x = 17; /* One form of indirect addressing shown */
place->y = 19; /* Another form of indirect addressing shown */
printf("Tallet er %i\n",place->x + place->y);
free( place ); /* Always remember to free allocated memory (Good habbit)*/
return( 0 );
}
Structures in function calls
To use structures in function calls, a pointer to the structure is used in the function call. The function therefore works on the original structure and not a copy.
#include <stdio.h>
#define TIMEZONE 1 // TIMEZONE from GMT in hours
struct TimeStruct { // Declare structure TimeStruct
int sec; // seconds 00 to 59
int min; // minutes 00 to 59
int hour; // hours 00 to 23
int mday; // day of the month 1 to 31
int mon; // month 1 to 12
int year; // year 1970 to 2106
};
adjustTimezone( struct TimeStruct *t ) {
t->hour += TIMEZONE; // Add TIMEZONE hours -> means via pointer
//Code to check if t->hour < 0 or > 23
}
int main( void ) {
struct TimeStruct t; // Define struct t as type TimeStruct
// Initialize time to time 00:00:00 date 1. january 1970
t.sec = 0;
t.min = 0;
t.hour = 0;
t.mday = 1;
t.mon = 1;
t.year = 1970;
adjustTimzone( &t );
printf("The time is %02i:%02i:02i\n", t.hour, t.min, .t.sec); //Will print 00:01:00 in this example
}
Arrays of structures example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STUDENTS 16
#define FREE 0
#define POPULATE(FIELD, ID, NAME, SURNAME) class[FIELD].id = ID, \
strcpy(class[FIELD].name,NAME), \
strcpy(class[FIELD].surname,SURNAME)
struct person {
int id;
char name[25];
char surname[25];
};
typedef struct person p;
int main( void ) {
p class[ STUDENTS ];
/* Initialize p*/
int i;
for ( i = 0; i < STUDENTS ; i++ ) {
class[i].id = FREE; /* 0 = No student allocated */
}
/* Populate class */
class[0].id = 101; strcpy(class[0].name,"Henrik"); strcpy(class[0].surname,"Thomsen");
class[7].id = 145; strcpy(class[7].name,"Anders"); strcpy(class[7].surname,"And");
POPULATE(3,78,"Kaptajn","Haddock"); /* using macro POPULATE (Just for fun)*/
/* Parse structure and print populated students */
for ( i = 0; i < STUDENTS ; i++ ) {
if ( class[i].id != FREE ) {
printf("Student: ID: %i name: %s %s\n",class[i].id, class[i].name, class[i].surname);
fflush(stdout); /* Empty buffer to stdout (Just for fun) */
}
}
}