Structure in C: Example
Example Of Structure
You will never get the size of a structure exactly as you think it must be. The sizeof function returns the size of structure larger than it is because the compiler pads struct members so that each one can be accessed faster without delays. So you should be careful when you read the whole structure from file which were written from other programs.
example of using C structure:
#include <stdio.h>
typedef struct _student{
char name[50];
unsigned int mark;
} student;
void print_list(student list[], int size);
void read_list(student list[], int size);
void main(){
const int size = 3;
student list[size];
read_list(list,size);
print_list(list,size); }
void read_list(student list[], int size)
{
printf("Please enter the student information:\n");
for(int i = 0; i < size;i++){
printf("\nname:");
scanf("%S",&list[i].name);
printf("\nmark:");
scanf("%U",&list[i].mark);
}
}
void print_list(student list[], int size){
printf("Students' information:\n"); [an error occurred while
processing this directive]
for(int i = 0; i < size;i++){
printf("\nname: %s, mark: %u",list[i].name,list[i].mark);
}
}
Here is program's output
Please enter the student information:
name:Jack
mark:5
name:Anna
mark:7
name:Harry
mark:8
Students' information:
name: J, mark: 5
name: A, mark: 7
name: H, mark: 8