본문 바로가기

공부/C언어

(36)
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을 불러오면 오류가 생깁니다. 결과화면
23. 포인트 문자열 #include void main(void) { char* str_test1 = "1234"; str_test1 = 0x30; printf("%s\r\n",str_test1); } 이런식으로하면 오류가 나버린다.... 이유는 상수로 선언이 되었기 때문이랍니다.. 그래서 상수로만 접근이 가능합니다. 어쨌든 변수로써 각각 메모리에 접근하려면 배열을 이용해야합니다. void main(void) { char str_test1[] = "1234"; char* str_test1_addr = str_test1; *str_test1_addr = 0x30; printf("%s\r\n",str_test1); }
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..
21. *(int*) 응용 main.c #include void main(void) { int test1 = 10; int test2 = 0; test2 = &test1; *(int*)test2 = 23; printf("test1=%d\r\n", test1); printf("test2=0x%x\r\n", test2); } *(int*) 을 통해서 다른 변수의 값에 영향을 줄수 있다. 결과화면
20. visual studio에서 sprintf 사용하기 main.c #include union converter { float f_val; short u_val[2]; }; void main(void) { float test1 = 40.26; char PID_val[50] = {0,}; union converter conv; conv.f_val = test1; printf("%x %x\r\n", conv.u_val[1], conv.u_val[0]); sprintf(PID_val, "TEST %04X%04X", conv.u_val[1],conv.u_val[0]); printf("\r\n%s\r\n", PID_val); } 1. 처음에 위에 코드대로 돌리면 아래와 같은 오류가 뜬다. 오류 C4996 'sprintf': This function or variabl..
19. union 응용 main.c #include union converter { float f_val; short u_val[2]; }; void main(void) { float test1 = 40.26; union converter conv; conv.f_val = test1; printf("%x %x\r\n", conv.u_val[1], conv.u_val[0]); } union은 메모리를 공유하는 구조 입니다. float 는 4바이트..short는 2바이트 입니다. float ㅁㅁ ㅁㅁ u_valㅁㅁ ㅁㅁ 이런식입니다. 결과 화면
18. typedef enum 응용