본문 바로가기

C언어

(7)
29. union 메모리 분할 하기 union 안에 sturct 를 넣어서 공용체의 메모리를 비트로 분할 하는 방법입니다. union 안에 있는 변수는 모두 union안에 공용으로 메모리가 할당 됩니다. 그걸 struct를 이용하여 비트 단위로 변수를 넣을수 있습니다. #include union Test_H { unsigned char value; struct { unsigned char MODE0:1; unsigned char MODE1:3; unsigned char MODE2:4; }S_Test_H; }; enum mode_num { mode0_1=0, mode0_2, mode0_3, mode0_4 }; #include "head_t.h" void main(void) { union Test_H iTest_H; iTest_H.S_Test..
28. CRC-16 예제 소스 #include unsigned int crc16(char* msg,unsigned int len) { int crc_val = (*msg) ^ 0xffff; char HI_crc = 0; char LO_crc = 0; int i, j = 0; for (i = 0; i >1) ^ 0xA001; //printf("P1:%x\n", crc_val); } else { cr..
27. 구조체 와 함수 Test_head.h #include typedef struct { int a; int b; }str_val; main.c #include "Test_head.h" void test1(str_val str_val1){ int test1_v1 = str_val1.a; printf("str_val = %d \r\n" , test1_v1); } void main(void){ str_val str_val2; str_val2.a = 10; test1(str_val2); }
26. static 구조체 static 안한 상태의 구조체 -구조체 주소를 찾아가면 0x36f848 을 보면 10 값을 가지고 있다가 함수에 빠져나가면 값이 사라지는 것을 볼 수 있다. 그러나 static 선언을 하면 .... 0xb8a188 주소에서 10(0xa)값을 가지고 있다가 함수를 나와도 값을 그대로 가지고 있는 것을 볼수 잇다. head.h #include typedef struct { int a1; int b1; }test_str1; void sub1(void); void sub2(void); void sub3(void); main.c #include "head.h" void main(void) { sub1(); sub3(); sub2(); } void sub1(void) { static test_str1 test_s..
25. 구조체_구조체 함수_구조체 포인트 main.c #include "head.h" #include "sub1_head.h" static TS_DrvTypeDef *TsDrv; void main(void) { if (stmpe811_ts_drv.ReadID(0x0811) == 0x0811) { TsDrv = &stmpe811_ts_drv; } TsDrv->Init(0x0811); } sub1.c #include "sub1_head.h" TS_DrvTypeDef stmpe811_ts_drv = { stmpe811_Init, stmpe811_ReadID, }; void stmpe811_Init(int DeviceAddr) { printf("Device_addr=0x%x\r\n", DeviceAddr); } int stmpe811_ReadID(in..
24. static void 함수 1.head_t.h #include void sub1_view(void); void sub1(void); 2.sub.c #include "head_t.h" void sub1_view(void) { sub1(); } static void sub1(void) { printf("View Sub1\r\n"); } 3.main.c void main(void) { sub1_view(); } 함수 앞에 static을 쓰게 되면 함수가 포함된 파일 안에서만 쓸수 있습니다. 그래서 main.c에서 sub1을 불러오면 오류가 생깁니다. 결과화면
22. 구조체 배열 변수 선언 1.head_t.h #include typedef struct { int TextColor; int BackColor; }test1; #define test_val 2 2. main.c #include "head_t.h" static int act_val = 0; test1 test1_1[test_val]; void main(void) { act_val = 0; test1_1[act_val].BackColor = 10; printf("test[0] = %d\r\n", test1_1[0].BackColor); printf("test[1] = %d\r\n", test1_1[1].BackColor); printf("test[2] = %d\r\n", test1_1[2].BackColor); act_val = 1..