[C언어/포인터] 컴파일러 경고 메세지 원인을 잘 모르겠습니다
글쓴이: gurumong / 작성시간: 수, 2016/09/28 - 1:19오전
링크드 리스트와 관련한 간단한 예제인데요
호환되지 않는 포인터 타입 초기화라는 경고가 몇개가 나왔습니다
요즘 컴파일러는 똑똑하다고 하니까 아무 이유없이 이런 경고를 내뱉지는 않을텐데
아무리 봐도 양쪽은 타입이 같은데
어떻게 해서 호환되지 않는 포인터 타입 초기화라고 할까요...?
#include <stdio.h> #include <stdlib.h> typedef struct linkedList { int *dataPtr; struct linkedNode *link; } *t_node; void createNode(t_node *ptr, int value); int main(void) { t_node node = NULL; createNode(&node, 7); createNode(&node, 75); t_node firstNode = node; t_node secondNode = firstNode->link; // <--------------------------------- 이 부분 경고! printf("첫번째 노드의 값: %d\n", *(firstNode->dataPtr)); printf("두번째 노드의 값: %d\n", *(secondNode->dataPtr)); } void createNode(t_node *ptr, int value) { t_node newNode = NULL; int *data = NULL; newNode = malloc(sizeof(struct linkedList)); data = malloc(sizeof(int)); newNode->dataPtr = data; *(newNode->dataPtr) = value; newNode->link = NULL; if(*ptr == NULL) *ptr = newNode; else { t_node nextPtr = *ptr; while (nextPtr->link != NULL) nextPtr = nextPtr->link; // <---------------------------------- 이 부분 경고! nextPtr->link = newNode; // <---------------------------------- 이 부분 경고! } }
----------------------------------------------
$ gcc linkedListSample.c linkedListSample.c: In function ‘main’: linkedListSample.c:20:23: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types] t_node secondNode = firstNode->link; ^ linkedListSample.c: In function ‘createNode’: linkedListSample.c:44:15: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] nextPtr = nextPtr->link; ^ linkedListSample.c:45:19: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] nextPtr->link = newNode; ^
Forums:
데이터 형이 달라서 나오는 경고 입니다.
데이터 형이 달라서 나오는 경고 입니다.
t_node secondNode = firstNode->link
struct linkedList = struct linkedNode *
이렇게 넣을려고 해서 나오는거에요.
댓글 달기