Created
November 10, 2012 06:15
-
-
Save Jack2/4050136 to your computer and use it in GitHub Desktop.
[C++]void_pointer_example
This file contains hidden or 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 <iostream> | |
using namespace std; | |
void increase(void* data, int ptr_size) | |
{ | |
if(ptr_size == sizeof(char)) | |
{ | |
char* ptr_char; | |
ptr_char = (char*)data; | |
++(*ptr_char); | |
} | |
else if (ptr_size == sizeof(int)) | |
{ | |
int* ptr_int; | |
ptr_int = (int*)data; | |
++(*ptr_int); | |
} | |
} | |
int main() | |
{ | |
char ch = 'A'; | |
int soo = 31337; | |
increase(&ch, sizeof(ch)); | |
increase(&soo, sizeof(soo)); | |
cout << "A 다음 문자는 : " << ch << endl; | |
cout << "31337 다음 수는 : " << soo << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
: type 에 관계없이 어떤 data type 이든지 가리킬 수 있다.
: 각각의 data type 마다 포인터를 모두 만든다면 메모리 낭비 발생뿐만 아니라 소스코드가 길어지게 되는데
void 포인터를 사용하여 이러한 문제를 해결할 수 있다.
(type casting 없이는 참조할 수 없다!)