포인터 + 포인터 에서 컴파일 에러 문제입니다.

powerc20의 이미지

포인터 + 포인터에서 에러가 발생하는 이유를 알고 싶어 글 남깁니다. 에러 종류( error: invalid operands to binary )

아래는 제가 테스트 해 본 파일과 실행 결과입니다.

컴파일 환경은

[powerc20@step09 app]# gcc -v
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux
Thread model: posix
gcc version 3.4.3 20050227 (Red Hat 3.4.3-22.1)

입니다.

<test-1.c>

#include <stdio.h>
int main(void)
{
int k,l;
unsigned char *i, *j;
i=(unsigned char *)0x10;
j=(unsigned char *)0x20;

printf(" i : %x, j : %x\n",i,j);
i = (i+(unsigned int)j);
printf(" i : %x, j : %x\n",i,j);

}

컴파일 에러
test.c: In function `main':
test.c:9: warning: unsigned int format, pointer arg (arg 2)
test.c:9: warning: unsigned int format, pointer arg (arg 3)
test.c:10: error: invalid operands to binary + <---------- 이 에러의 원인이 궁금합니다.
test.c:11: warning: unsigned int format, pointer arg (arg 2)
test.c:11: warning: unsigned int format, pointer arg (arg 3)
test.c:4: warning: unused variable `k'
test.c:4: warning: unused variable `l'

<test-2.c>

#include <stdio.h>
int main(void)
{
int k,l;
unsigned char *i, *j;
i=(unsigned char *)0x10;
j=(unsigned char *)0x20;

printf(" i : %x, j : %x\n",i,j);
i = (i+(unsigned int)j);
printf(" i : %x, j : %x\n",i,j);

}

실행 결과
#./test-2
i : 10, j : 20
i : 30, j : 20

doldori의 이미지

test-1.c에서
i+(unsigned int*)j
로 쓰시지 않았나요? 그렇다면 정의되지 않은 연산이므로 에러입니다. 이항 + 연산은
(산술형) + (산술형), (포인터형) + (정수형), (정수형) + (포인터형)에 대해서만
정의됩니다.

ps. 코드의 의도를 전혀 모르겠군요. -.-a