포인터, 배열, 구조체 연산
Language/C 2012. 1. 11. 16:42
[포인터 연산]
(일반 배열)
(일반 배열)
p[i]
*(p+i)
(구조체 배열)
p[i].idx
*(p+i).idx
(p+i)->idx
소스
결과
/*
============================================================================
Name : pointer.c
Author :
Version :
Copyright :
Description : pointer in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <string.h>
struct mystruct{
char str[10];
int ii;
};
int main(void)
{
struct mystruct mystruct;
struct mystruct* pmystruct;
pmystruct=&mystruct;
strcpy(mystruct.str,"test");
mystruct.ii=45;
printf("mystruct.str: %s\n",mystruct.str);
printf("mystruct: %d\n",mystruct.str);
struct mystruct* str = mystruct.str;
printf("str->ii: %d\n",str->ii);
printf("\n");
printf("mystruct.ii: %d\n",mystruct.ii);
printf("pmystruct->ii: %d\n",pmystruct->ii);
printf("pmystruct[0].ii: %d\n",pmystruct[0].ii);
printf("(pmystruct+0)->ii: %d\n",(pmystruct+0)->ii);
printf("\n");
printf("mystruct: %d\n",mystruct);
printf("&mystruct: %d\n",&mystruct);
printf("pmystruct: %d\n",pmystruct);
printf("\n");
int arrint[5]={1,2,3,4,5};
int* parrint = arrint;
printf("arrint: %d\n",arrint);
printf("arrint[0]: %d\n",arrint[0]);
printf("parrint[0]: %d\n",parrint[0]);
printf("*(parrint): %d\n",*(parrint));
printf("*(arrint): %d\n",*(arrint));
return 0;
}
결과
mystruct.str: test
mystruct: 2293600
str->ii: 45
mystruct.ii: 45
pmystruct->ii: 45
pmystruct[0].ii: 45
pmystruct+0.ii: 45
mystruct: 1953719668
&mystruct: 2293600
pmystruct: 2293600
arrint: 2293552
arrint[0]: 1
parrint[0]: 1
*(parrint): 1
*(arrint): 1
'Language > C' 카테고리의 다른 글
multicast(receive) (0) | 2012.01.16 |
---|---|
multicast(send) (0) | 2012.01.16 |
html 태그 제거 (0) | 2012.01.05 |
C로 정규식라이브러리 사용 (0) | 2012.01.05 |
패턴 매칭 ( Pattern Matching ) (0) | 2012.01.05 |