동적할당
void *
특정위치의 저장
포인터함수
2. void *
오직 주소값만 가짐
다른 포인터와 달리 자료형에 대한 정보가 없음
포인터연산 및 메모리 참조 불가
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <pthread.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <vector> | |
#include <iostream> | |
#define MAX_THREAD_NUM 20 | |
using namespace std; | |
void *func1(void *) { | |
printf("Thread 1\n"); | |
pause(); | |
} | |
void *func2(void *) { | |
printf("Tread 2\n"); | |
pause(); | |
} | |
void *func3(void *) { | |
printf("Thread 3\n"); | |
pause(); | |
} | |
void *func4(void *) { | |
prinft("Thread 4\n"); | |
pause(); | |
} | |
int main() { | |
// 인자가 함수포인터인 vector 생성 | |
vector<void *(*)(void *)> thread_list; | |
vector<pthread_t> tident(MAX_THREAD_NUM); | |
int status; | |
thread_list.push_back(func1); | |
thread_list.push_back(func2); | |
thread_list.push_back(func3); | |
thread_list.push_back(func4); | |
cout << "등록된 쓰레드 " << thread_list.size() << end1; | |
for(int i = 0; i < thread_list.size(); i++) { | |
pthread_create(&tident[i], NULL, thread_list[i], (void *)NULL); | |
} | |
cout << "thread Join Wait" << end1; | |
for(int i = 0; i < tident.size(); i++) { | |
pthread_join(tident[i], (void **_ &status); | |
} | |
return 1; | |
} |
3. 포인터 함수
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
// Function Pointer 포인터 함수 | |
typedef int (*calculatorFunctionPointer)(int, int); | |
// Plus Function 덧셈 함수 | |
int plus (int argument1, int argument2) { | |
return argument1 + argument2; | |
} | |
// Minus Function 뺄셈 함수 | |
int minus (int argument1, int argument2) { | |
return argument1 - argument2; | |
} | |
// Multiple Function 곱셈 함수 | |
int multiple (int argument1, int argument2) { | |
return argument1 * argument2; | |
} | |
// Division Function 나눗셈 함수 | |
int division (int argument1, int argument2) { | |
return argument1 / argument2; | |
} | |
// 매개변수로 함수포인터를 갖는 calculator 함수 | |
int calculator (int argument1, int argument2, calculatorFunctionPointer func) { | |
return func(argument1, argument2); | |
} | |
int main(int argc, char** argv) { | |
calculatorFunctionPointer calc = NULL; | |
int num1 = 0, num2 = 0; | |
char op = 0; | |
int result = 0; | |
scanf(“cal : %d %c %d”, &num1, &op, &num2); | |
switch(op) { | |
case ‘+’: | |
calc = plus; | |
break; | |
case ‘-’: | |
calc = minus; | |
break; | |
case ‘*’: | |
calc = multiple; | |
break; | |
case ‘/’: | |
calc = division; | |
break; | |
} | |
result = calculator(num1, num2, calc); | |
printf(“result : %d”, result); | |
return 0; | |
} |