기초 질문입니다.
글쓴이: jee89 / 작성시간: 화, 2003/01/14 - 4:24오후
#include <stdio.h>
typedef struct _ts {
char name[20];
int key;
} st_list_t ;
st_list_t *headp , *lastp;
int main()
{
if ( headp == lastp == NULL )
fprintf( stderr , "all Null Pointer\n" );
else
fprintf( stderr , "something wrong\n" );
}
이상을 실행하면
all Null Pointer 이 아니라
something wrong이 나옵니다.
headp , lastp가 전역외부변수이므로 0값을 가지므로
all Null Pointer가 찍혀야 맞지 않나요?
Forums:
연산 순서가 잘못된 거 같군요.
headp == lastp == NULL 을
headp == NULL && lastp == NULL 으로 하셔야 합니다.
headp == lastp == NULL 으로 하시면
( (headp == lastp) == NULL) 의 순서로 연산이 진행됩니다.
전역변수의 사용은 되도록이면 삼가하시는것이 좋을듯 싶군여.전역변수
전역변수의 사용은 되도록이면 삼가하시는것이 좋을듯 싶군여.
전역변수 선언시 초기화를 하시지 않았군여..
컴파일러가 마음대로 어떤 값을 넣어 버릴 수도 있습니다.
자신이 원하는 결과를 얻고자 한다면 선언시 NULL ( (void *)0)로
초기화 시켜 줘야지만 됩니다. ^^;
전역변수 초기화에 대해서 한마디...
-> 전역변수는 초기화시키지 않아도 됩니다. 자동으로 NULL로 초기화 됩니다. 이는 지역변수가 stack에 생성되는 것에 반하여, 전역변수는 Data영역중, Initialized data영역에 생성되기 때문입니다. ^^;
댓글 달기