Difference between revisions of "C programming/Structures"
From Teknologisk videncenter
m (→Arrays of structures example) |
m (→Arrays of structures example) |
||
Line 78: | Line 78: | ||
#define STUDENTS 16 | #define STUDENTS 16 | ||
#define FREE 0 | #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 { | struct person { | ||
− | + | int id; | |
− | + | char name[25]; | |
− | + | char surname[25]; | |
}; | }; | ||
Line 87: | Line 92: | ||
int main( void ) { | 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"); /* Just for fun using macro POPULATE */ | ||
− | |||
− | |||
− | |||
− | + | /* 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) */ | |
− | + | } | |
− | + | } | |
} | } | ||
</source> | </source> | ||
[[Category:C]] | [[Category:C]] |
Revision as of 16:01, 21 November 2010
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 );
}
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"); /* Just for fun using macro POPULATE */
/* 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) */
}
}
}