jazykc
2013.3
upoljazykc
Main Page
Related Pages
Classes
Files
File List
File Members
All
Classes
Files
Functions
Variables
Typedefs
Macros
Pages
strukturovane-datove-typy
strukt.c
Go to the documentation of this file.
1
5
#include <stdio.h>
6
#include <stdlib.h>
7
#include <stddef.h>
//offsetof()
8
9
struct
osoba_s
{
10
char
jmeno[20];
//v inicializatoru se pak pokusime inicializovat delsi retezcem
11
int
cislo;
12
};
13
14
//offset clena struktury pocita makro offsetof() stddef.h
15
16
//ruzny padding mezi cleny struct-ury, GCC, 64bit: LP64 platoforma
17
struct
datum
{
char
den;
char
mesic;
short
rok; } ;
//sizeof == 4
18
struct
datum1
{
char
den;
short
rok;
char
mesic; } ;
//sizeof == 6 misto 4 !!!
19
struct
datum6
{
char
den;
char
mesic;
int
rok ; } ;
//sizeof 8 misto 6 (1+1+4) !!!
20
struct
ilil
{
int
i1;
long
l1;
int
i2;
long
l2; } ;
//sizeof 32 misto 24 (4+8+4+8) !!!
21
struct
iill
{
int
i1;
int
i2;
long
l1;
long
l2; } ;
//sizeof 24
22
23
//union: cleny struktury se v pameti prekryvaji:
24
union
ruzne_u
{
25
short
s;
26
int
i;
27
long
l;
28
struct
datum
datum;
29
char
ch4[4];
30
char
ch8[8];
31
} ruzne;
32
33
34
35
int
main() {
36
37
//da endian se zjistit jen pomoci maker? bez promenne?
38
static
union
endian_s {
39
short
s;
40
char
ch2[2];
41
} endian;
42
43
struct
osoba_s
o = {
"01234567890123456789ABC"
};
//inicializace prepise .cislo? => warning: too long ...
44
45
struct
datum
d1 = {1,2,2048};
//zkusime pretypovat na int
46
47
endian.s =
'L'
*256 +
'E'
;
48
49
printf(
"endian: %c%c .s=%hx\n"
, endian.ch2[0], endian.ch2[1], endian.s);
50
51
printf(
"o.jmeno=\"%s\"\n"
, o.jmeno);
52
printf(
"o.cislo=%i\n"
, o.cislo);
53
54
printf(
"sizeof(int) = %zu\n"
,
sizeof
(
int
));
55
printf(
"sizeof(long) = %zu\n"
,
sizeof
(
long
));
56
printf(
"sizeof(struct datum) = %zu\n"
,
sizeof
(
struct
datum
));
57
printf(
"sizeof(struct datum1) = %zu\n"
,
sizeof
(
struct
datum1
));
58
printf(
"datum1.rok offset: %zu\n"
, offsetof(
struct
datum1
, rok));
59
printf(
"datum1.mesic offset: %zu\n"
, offsetof(
struct
datum1
, mesic));
//padding je za poslednim clenem struct
60
printf(
"sizeof(struct datum6) = %zu\n"
,
sizeof
(
struct
datum6
));
61
printf(
"sizeof(struct ilil) = %zu\n"
,
sizeof
(
struct
ilil
));
62
printf(
"sizeof(int+long+int+long) = %zu\n"
, 2*(
sizeof
(
int
)+
sizeof
(
long
)));
63
printf(
"sizeof(struct iill) = %zu\n"
,
sizeof
(
struct
iill
));
64
65
//pretypovat struct datum na int nelze!!! pomoci (int)d1
66
//musime pouzit union:
67
ruzne.datum = d1;
68
printf(
"datum jako int=%u\n"
, ruzne.i);
69
printf(
"datum jako int=%x\n"
, ruzne.i);
//1,2,2048 pozpatku, na 99% little endian platforma! (intel/amd)
70
printf(
"datum jako short=%04x\n"
, ruzne.s);
71
return
0;
72
73
}
ruzne_u
Definition:
strukt.c:24
datum
Definition:
t19u2.c:36
iill
Definition:
strukt.c:21
datum1
Definition:
strukt.c:18
datum6
Definition:
strukt.c:19
ilil
Definition:
strukt.c:20
osoba_s
Definition:
strukt.c:9
Generated on Sat Dec 7 2013 22:57:26 for jazykc by
1.8.5