본문 바로가기

공부/C언어

(36)
17. 배열과 포인트 1. 첫번째 유형 main.c #include void test(char *pdata) { int i = 0; for (i = 0; i < 10; i++) { printf("%d", pdata[i]); } } void main() { char test_Val[10] = { 0,1,2,3,4,5,6,7,8,9 }; test(test_Val); } 배열을 함수에서 넣어줄 때 주소로 넘겨준다. 결과화면 2. 두번째 유형 main.c #include void sub1(int sub1_val) { int i = 0; int *sub1_val_addr = sub1_val; printf("sub1_val=%x\r\n", sub1_val); for (i = 0; i < 20; i++) { printf("sub1_val..
visual studio define 설정
16. 구조체 전역선언(extern) main.c #include "main.h" #include "header.h" test_str test_str1; extern test_str test_str1; void main(void) { test_str1.a = 10; printf("a=%d\r\n",test_str1.a); test_ext1(); printf("a=%d\r\n", test_str1.a); } main.h #ifndef _main_h #define _main_h #endif // !main_h header.h #ifndef _header_h #define _header_h #include typedef struct { int a; int b; }test_str; #endif // !_header.h sub1.c #include ..
15.배열 초기화 하기 3가지 방법 1. 소스 main.c #include #include //memset 헤더 void main(void) { int a[10] = { 0 }; int b[10] = { 0, }; int c[10]; memset(c, 0, 10); printf("a_%d b_%d c_%d",a[1],b[1],c[1]); } 2. 결과
14. 구조체 기본 1. 소스 test1.h #include void test2(void); typedef struct { char high_test[20]; char low_test[20]; }test_struct; main.c #include "test1.h" test_struct user_test; void main(void) { user_test.high_test[0] = 1; user_test.high_test[1] = 2; printf("main %d %d \r\n", user_test.high_test[0], user_test.high_test[1]); test2(); printf("main %d %d \r\n", user_test.high_test[0], user_test.high_test[1]); } tes..
13. typedef enum 1, main.c #include "test.h" void main(void) { number number1; number1 = key2; printf("test numer1 %d \r\n", number1); printf("test key1%d \r\n", key1); printf("test key2%d \r\n", key2); } 2. test.h #include typedef enum { key1 = 0, key2, key3 }number; 3. 결과 4. // 주석 이렇게 쓰면 장점이 1. 따로 변수 설정 안해도 되요~ 2. 구조체와 enum 의 장점을 다 쓸수 있어요~
12. 구조체와 포인트 관계 1. main.c #include "test1.h" void h_Test(void); test1 test2 = {10,11};test1 *test3; void main(void){test3 = &test2;printf("test\n");h_Test();} void h_Test(void) {printf("test3 a = %d\n", test3->a);} 2. test1.h #include typedef struct{int a;int b;}test1;
11. static void 의 쓰임 1. 사용 예 H_test.h #include main.c #include "H_test.h" void main(void) { ex_test(); } sub.c #include "H_test.h" void ex_test(void) { printf("ex_test\n"); } 이런 식으로 코드를 작성한다고 한다면 static 이 없으면 main.c 에서 sub.c의 ex_test를 가져다 쓸수 있음 하지만 sub.c #include "H_test.h" static void ex_test(void) { printf("ex_test\n"); } 이렇게 수정해주면 main.c에서 가져다 쓸수 없고 sub.c 안에서만 쓸수 있습니다.