#include <stdio.h>
typedef struct _graduation {
char CollegeName[100];
char Title[100];
} Graduation_t;
typedef struct _address {
char Street[100];
} Address_t;
typedef struct _serial {
int Age;
char *Name;
char LastName[20];
Address_t Address;
int Notas[10];
Graduation_t Graduations[100];
} Person_t;
/* memory aligment directive pragma - try remove pragma, and move y right below x and verify the changes of sizeof(struct point)
This may slow down the speed of the program because of the compiler tricks to change memmory access
*/
struct point1 {
int x;
char value1;
int y;
char value2;
};
struct point2 {
int x;
int y;
char value1;
char value2;
};
#pragma pack(1)
struct point3 {
int x;
char value1;
int y;
char value2;
};
#define NAME 2
int main() {
Person_t someone = {
.Age = 20,
.Name = "Thiago",
.LastName = { [0] = 'M', [4] = 'i'},
.Address = {
.Street = "Rua Xpto"
},
.Notas = { [0 ... 5] = 10, [6 ... 8] = 5, [9] = 2},
.Graduations = { [0].CollegeName = "Unicamp", [0].Title = "Computer Engineer",
[1].CollegeName = "Unicamp", [1].Title = "Bachelor in Computer Science"
}
};
int whitespace[256] = { [' '] = 1, ['\t'] = 1, ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 };
struct point1 ptarray[10] = { [2].y = 12, [2].x = 45, [0].x = 60 };
char vetor[20];
char matriz[] = { 'A', 'B'};
char matriz2[] = { "SENAI BOM DIA" };
printf("Name: %s, Age : %d, Street: %s\n", someone.Name, someone.Age, someone.Address.Street);
sprintf(vetor, "%.*s", sizeof(matriz), matriz);
printf("%.*s \n",2,vetor);
printf("Sizeof Struct point: %d\n", sizeof(struct point1));
printf("Sizeof Struct point: %d\n", sizeof(struct point2));
printf("Sizeof Struct point: %d\n", sizeof(struct point3));
return 0;
}
No comments:
Post a Comment