본문 바로가기
[IT] C언어 입문(14, 끝) C 전처리기(C preprocessor), 함수 포인터(function pointer) □ C preprocessor, function pointer * List Creation and Element Counting - element를 포인터를 따라가면서 카운팅... recursion 이용 int count(LINK head) { if (head == NULL) return 0; else return (1 + count(head->next)); } recursion 이용 안 함 int count(LINK head) { int cnt = 0; for(; head!=NULL; head=head->next) // head=head->next는 head가 다음 element를 포인팅하는 식 { cnt++; } return cnt; } --- LINK stringToList(char s[]) { L.. 2021. 9. 18.
[IT] C언어 입문(13) 자료구조(data structure) 예시 - 연결리스트(linked list) * Unions - The same syntax as structures - But, members share storage - Union은 Structure에 비해 메모리 공간을 아낄 수는 있으나 요즘엔 그닥... - 어떤 변수로 액세스 하느냐에 따라서 달라짐;; structure와 union의 차이점 --- struct foo { int n; float r; } p; ==> p n | | r | | union foo { int n; float r; } q; ==> q n, r | | --- typedef union foo { int n; float r; } number; // number type 정의 int main(void) { number m; // number type 선언 m.n = 2345.. 2021. 9. 14.
[IT] C언어 입문(12) structure, union, enumerated types - 구조체, 공용체, 열거체 □ structure / union / enumerated types * Arguments to main() - To communicate with the OS - argc: the number of command line arguments // argc, argy 변수의 이름은 관례이므로 바꿔도 오류는 안 남 - argv: an array of strings - The strings are the words that make up the command line - argv[0] contains the name of the command itself - 사용자가 인자를 프로그램에 직접 주기 위해 사용, 명령 프롬프트! --- #include int main(int argc, char *argv[]) { .. 2021. 9. 11.
[IT] C언어 입문(11) File operation 파일연산, String, 다차원 배열 예시 □ file operations * calloc() and malloc() 함수 => 동적 메모리 할당 - In the standard library (stdlib.h) - To dynamically create storage for arrays, structures, and unions - void* calloc( size_t n, size_t s ) - Contiguous allocation : Allocates contiguous space in memory with a size of n * s bytes - The space is initialized with 0% - If successful, returns a pointer to the base if the space - Otherwise, r.. 2021. 9. 2.
[IT] C언어 입문(10) Pointer, 역참조, swap 함수 활용, 배열과 포인터 비교 □ Pointer, 포인터 * Pointer dereferencing(포인터 역참조) - Obtaining the value pointed to by a pointer a = *x; // x라는 포인터가 가리키는 변수 값을 a에 저장 *x = a; // a라는 값을 x가 가리키고 있는 변수 값에 저장 - if a NULL pointer is dereferenced then a runtime error will occur and execution will stop likely with a "segmentation fault" - %p : the printf format to print the value of a pointer * Swap function - p와 q의 값을 서로 바꿔주는 swap 함수 예제.. 2021. 8. 31.