Difference between revisions of "C programming/Structures"
From Teknologisk videncenter
m (→Example 3) |
m (→Example 3) |
||
Line 68: | Line 68: | ||
free( place ); /* Always remember to free allocated memory (Good habbit)*/ | free( place ); /* Always remember to free allocated memory (Good habbit)*/ | ||
return( 0 ); | return( 0 ); | ||
+ | } | ||
+ | </source> | ||
+ | = Arrays of structures example = | ||
+ | <source lang=c> | ||
+ | #include <string.h> | ||
+ | #define STUDENTS 16 | ||
+ | #define FREE 0 | ||
+ | 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"); | ||
+ | |||
+ | /* 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 15:38, 21 November 2010
Contents
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 <string.h>
#define STUDENTS 16
#define FREE 0
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");
/* 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) */
}
}
}