Informatyka Wy-05 Struktury, unie, organizacja pól struktur w pamięci mgr inż. Krzysztof Kołodziejczyk krzysztof.m.kolodziejczyk@pwr.edu.pl 27.03.2019
Strona kursu http://w12.pwr.wroc.pl/inf/
Konsultacje Piątek 13:00 C2/111a
Konspekt
Konspekt
Tablice i n t a r r [ 1 2 ] ; // a r r [ 4 ] === ( a r r +4)
Wskaźnik i n t a ; i n t a_p = &a ; a_p = 5 ; // a === 5
Parametry funkcji void swap ( i n t a, i n t b ) ;
Parametry funkcji void swap ( i n t a, i n t b ) ; i n t iloczyn_wektorow ( double x1, double y1, double z1, double x2, double y2, double z2, double x3, double y3, double z3 ) ;
Alokacja pamięci i n t a = 0 ; i n t b = malloc ( s i z e o f ( i n t ) ) ;
fwrite i n t a r r [ 1 2 ] ; f w r i t e ( arr, s i z e o f ( i n t ), 1 2, f ) ;... i n t a r r = c a l l o c (12, s i z e o f ( i n t ) ) ; f r e a d ( arr, s i z e o f ( i n t ), 1 2, f ) ;
Konspekt
Struktury s t r u c t { i n t v1 ; i n t v2 ; } ;
Struktury s t r u c t s t r u k t u r a { i n t v1 ; i n t v2 ; } ; s t r u c t s t r u k t u r a x ;
Struktury s t r u c t {... } x ; i n t x ;
Struktury s t r u c t s t r u k t u r a { i n t v1 ; i n t v2 ; } x ; x. v1 = 5 ; x. v2 = 7 ;
Wskaźniki na struktury s t r u c t s t r u k t u r a { i n t v1 ; i n t v2 ; } x ; s t r u c t s t r u k t u r a x_p = &x ;
Wskaźniki na struktury s t r u c t s t r u k t u r a { i n t v1 ; i n t v2 ; } x ; s t r u c t s t r u k t u r a x_p = &x ; i n t x, y ; i n t x, y ;
Wskaźniki na struktury i n t a = 5 ; i n t b = 6 ; s t r u c t s t r u k t u r a { i n t v1 ; i n t v2 ; } x ; x. v1 = &a ; x. v2 = &b ;
Wskaźniki na struktury i n t a = 5 ; i n t b = 6 ; s t r u c t s t r u k t u r a { i n t v1 ; i n t v2 ; } x ; x. v1 = &a ; x. v2 = &b ; s t r u c t s t r u k t u r a x_p = &x ; // x_p. v1
Kolejność wykonywania operacji 1 a++ a-- func() arr[]. -> 2 ++a --a +a -a! (type) * & 3 * / % 4 + -
Wskaźniki na struktury x_p. v1
Wskaźniki na struktury x_p. v1 ( x_p ). v1 ; // i n t x. v1 ; //5 ( x_p ). v1 ; //5
Wskaźniki na struktury x_p. v1 ( x_p ). v1 ; // i n t x. v1 ; //5 ( x_p ). v1 ; //5 x_p >v1 ; // i n t x_p >v1 ; //5
Pytania s t r u c t d i a l o g { char pytanie ; char odpowiedz ; } dlg ; p r i n t f ( " Pytania?\n" ) ; s c a n f ( "%ms",& dlg. pytanie ) ;
Mieć pamięć do struktur s t r u c t s t r u k t u r a { i n t v1 ; i n t v2 ; } a r r [ 1 2 ] ; a r r [ 5 ]. v1 = 5 ; a r r [ 5 ]. v2 = 7 ;
Mieć pamięć do struktur s t r u c t s t r u k t u r a { i n t v1 ; i n t v2 ; } a r r [ 1 2 ] ; a r r [ 5 ]. v1 = 5 ; a r r [ 5 ]. v2 = 7 ; ( ( a r r +5)). v1 = 5 ; ( a r r+5) >v1 = 5 ;
Konspekt
Mieć pamięć do struktur 66 c 9 3 a e 0 0000 0000 0000 0000 [ 0005 0000 0006 0000] 66 c 9 3 a f 0 0000 0000 0000 0000 0000 0000 0000 0000 66 c93b00 0000 0000 0000 0000 0000 0000 0000 0000 s t r u c t { i n t v1 ; i n t v2 ; } a r r [ 4 ] ; a r r [ 1 ]. v1 = 5 ; a r r [ 1 ]. v2 = 6 ;
Mieć pamięć do struktur s t r u c t point { i n t x ; i n t y ; } f i g u r e [ ] = { { 0, 0 }, { 0, 2 }, { 2, 2 }, { 2, 0 } } ;
Mieć pamięć do struktur s t r u c t point { i n t x ; i n t y ; } f i g u r e ; f i g u r e = c a l l o c ( s i z e o f ( s t r u c t point ), 4 ) ;
Mieć pamięć do struktur s t r u c t point { i n t x ; i n t y ; } f i g u r e ; f i g u r e = c a l l o c ( s i z e o f ( s t r u c t point ), 4 ) ; s t r u c t point i ; f o r ( i = &f i g u r e [ 0 ] ; i < &f i g u r e [ 4 ] ; i++ ){ i >x=0; i >y=0; }
Pytania s t r u c t d i a l o g { char pytanie ; char odpowiedz ; } pytania [ 3 ], i t e r ; p r i n t f ( " Pytania?\n" ) ; f o r ( i t e r=pytania ; i t e r <pytania +3; i t e r ++){ s c a n f ( "%ms",& i t e r >pytanie ) ; }
Lista s t r u c t l i s t { i n t value ; s t r u c t l i s t next ; } l i s t a ; l i s t a = malloc ( s i z e o f ( s t r u c t l i s t ) ) ; l i s t a >value = 5 ; l i s t a >next=null;
Lista s t r u c t l i s t { i n t value ; s t r u c t l i s t next ; } l i s t a ; l i s t a = malloc ( s i z e o f ( s t r u c t l i s t ) ) ; l i s t a >value = 5 ; l i s t a >next=null; s t r u c t l i s t i ; f o r ( i=l i s t a ; i >next ; i=i >next ) ;
Lista s t r u c t wektor { double x, y, z ; } ; i n t iloczyn_wektorow ( s t r u c t wektor v1, s t r u c t wektor v2, s t r u c t wektor v3 ) ;
Lista s t r u c t wektor { double x, y, z ; } ; s t r u c t wektor iloczyn_wektorow ( s t r u c t wektor v1, s t r u c t wektor v2, ) ;
Pytania s t r u c t d i a l o g { char pytanie ; char odpowiedz ; } pytania [ 3 ], i t e r ; p r i n t f ( " Pytania?\n" ) ; f o r ( i t e r=pytania ; i t e r <pytania +3; i t e r ++){ s c a n f ( "%ms",& i t e r >pytanie ) ; } odpowiedz ( pytania ) ; f o r ( i t e r=pytania ; i t e r <pytania +3; i t e r ++){ p r i n t f ( "%s : %s \n", i t e r >pytanie, i t e r >odpowiedz ) }
typedef typedef unsigned s h o r t s h o r t i n t uint8 ; uint8 x ;
typedef typedef unsigned s h o r t s h o r t i n t uint8 ; uint8 x ; typedef s t r u c t student_s { i n t index ; char given_name ; char last_name ; } Student ; s t r u c t student_s s1 ; Student s1 ;
typedef s t r u c t _IO_FILE { i n t _flags ; / Highorder word i s _IO_MAGIC; r e s t i s f l a g s. s t r u c t _IO_marker _markers ; s t r u c t _IO_FILE _chain ; i n t _ f i l e n o ; i n t _flags2 ; (... ) unsigned s h o r t _cur_column ; s i g n e d char _vtable_offset ; char _shortbuf [ 1 ] ; / char _save_gptr ; char _save_egptr ; / _IO_lock_t _lock ; } ; t y p e d e f _IO_FILE FILE ;
fwrite s t r u c t point { i n t x ; i n t y ; } f i g u r e ; f i g u r e = c a l l o c ( 4, s i z e o f ( s t r u c t point ) ) ; (... ) f w r i t e ( f i g u r e, s i z e o f ( s t r u c t point ), 4, f ) ;
fwrite typedef s t r u c t student_s { i n t index ; char given_name ; char last_name ; } Student ; Student c l a s s [ 4 0 ] ; ; f w r i t e ( c l a s s, s i z e o f ( student ), 4 0, f ) ;
Serializacja [ ] { " index " :123456, "given_name" : "Jan", "last_name" : " Kowalski " }, { " index " :234567, "given_name" : "Adam", "last_name" : "Nowak" }
Serializacja char s e r i a l i z e _ s t u d e n t ( student s ){ char s t r i n g = NULL; cjson index = cjson_createnumber ( s. index ) ; cjson gn = cjson_createstring ( s. given_name ) ; cjson l n = cjson_createstring ( s. last_name ) ; } cjson r e s u l t = cjson_createobject ( ) ; cjson_additemtoobject ( r e s u l t, " index ", index ) ; cjson_additemtoobject ( r e s u l t, "given_name", gn ) ; cjson_additemtoobject ( r e s u l t, "last_name", ln ) ; s t r i n g = cjson_print ( r e s u l t ) ; cjson_delete ( r e s u l t ) ; return s t r i n g ;
Konspekt
Unie union { i n t a ; f l o a t b ; } a r r [ 2 0 ] ;
Unie union { i n t a ; f l o a t b ; } x ; x. b=1; p r i n t f ( "%x\n", x. a ) ; //3 f800000
Unie typedef s t r u c t number_s { char type ; union { i n t i ; f l o a t f ; } value ; } Number ; Number a r r [ 3 ] ; a r r [ 0 ]. type = i ; a r r [ 0 ]. value = 1 0 ; a r r [ 1 ]. type = i ; a r r [ 1 ]. value = 2 0 ; a r r [ 2 ]. type = f ; a r r [ 2 ]. value = 2. 0 ;
Konspekt
MarchewkBitowe pole s t r u c t { i n t index ; unsigned i n t p r e s e n t : 1 ; unsigned i n t s l e e p i n g : 1 ; unsigned i n t is_woman : 1 ; unsigned i n t row_no : 5 ; } student_status ; student_status. p r e s e n t = 1 ; student_status. s l e e p i n g = 0 ; student_status. is_woman = 1 ; student_status. row_no = 1 ;
MarchewkBitowe pole #d e f i n e PRESENT 1 #d e f i n e SLEEPING 2 #d e f i n e IS_WOMAN 4 #d e f i n e ROW 8 #d e f i n e ROW_MASK 0xF8 s t r u c t { i n t index ; unsigned char f l a g s ; } student_status ; student_status. f l a g s = 0 ; student_status. f l a g s = SLEEPING ; student_status. f l a g s = 10 ROW; // ( student_status. f l a g s & ROW_MASK) / ROW
Bibliografia https://staff.uz.zgora.pl/jgramack/files/c/ansi_c_14.pdf https://en.cppreference.com/w/c/language/operator_precedence
Koniec Dziękuję za uwagę!