Last active
November 16, 2016 01:49
-
-
Save JeonghunLee/e059609200cabb0dca3972c13e17a3fc to your computer and use it in GitHub Desktop.
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> | |
#define CNT_TEST 2 | |
int map[20]={0}; // save address info | |
int size=0; | |
int map2[20]={0}; // save address info | |
int size2=0; | |
/* | |
for this pointer argument test | |
any pointer arguments can change that value but can't change pointer address value if just used one pointer (*) | |
if you want to change pointer address in sub function, try to use double pointer (**) | |
*/ | |
int setDataVoid(void *data) // cant change this point argument | |
{ | |
map[size++]=data; | |
printf("void * set addr=%x \n",data); | |
return 0; | |
} | |
int getDataVoid(void* data) // when you read void * data , there is a problem | |
{ | |
if(size==0) return -1; | |
data = (void *) map[--size]; | |
printf("void * get addr=%x \n",data); | |
return 0; | |
} | |
int getDataVoid2(void** data) // when you read void ** data , there is no problem | |
{ | |
if(size==0) return -1; | |
*data = (void *) map[--size]; | |
printf("void ** get addr=%x \n",*data); | |
return 0; | |
} | |
int getDataInt(int * data) // when you read int * data , there is a problem | |
{ | |
if(size2==0) return -1; | |
data = (int *) map2[--size2]; | |
printf("int * get addr=%x \n", data); | |
return 0; | |
} | |
int setDataInt(int *data) // cant change this point argument | |
{ | |
map2[size2++]=data; | |
printf("int * set addr=%x \n",data); | |
return 0; | |
} | |
int main(void) { | |
int data[]={10,11,12,13,14,15,16,17,18,19,20,21}; | |
int getdata=0; | |
int *pdata; | |
int i; | |
int cnt; | |
cnt = sizeof(data); | |
cnt = cnt > CNT_TEST ? CNT_TEST:cnt; | |
printf("Void * Pointer \n"); | |
for(i=0;i<cnt;i++) | |
setDataVoid((void *)&data[i]); | |
printf("\n"); | |
for(i=0;i<cnt;i++) | |
{ | |
getDataVoid((void*)&getdata); // can't get this value | |
printf("Void * after called get addr=%x \n",getdata); | |
} | |
printf("\nVoid** Pointer \n"); | |
getdata=0; | |
for(i=0;i<cnt;i++) | |
setDataVoid((void *)&data[i]); | |
printf("\n"); | |
for(i=0;i<cnt;i++) | |
{ | |
getDataVoid2((void*)&getdata); | |
pdata=getdata; | |
printf("Void ** after called get addr=%x data=%d \n",getdata,*pdata); | |
} | |
printf("\nINT Pointer \n"); | |
getdata=0; | |
for(i=0;i<cnt;i++) | |
setDataInt(&data[i]); | |
printf("\n"); | |
for(i=0;i<cnt;i++) | |
{ | |
getDataInt(&getdata); | |
// pdata=getdata; | |
printf("int * after called get addr=%x \n",getdata); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment