NULL 문자는 아스키 값 0과 같다.


#include <stdio.h>

int null_test(char* pstr)
{
    int lenth = 0;
    while(*pstr!=NULL)
    {
        pstr++;
        lenth++;
    }

    return lenth;
}

int main()
{
    char *str = "abcde";
    int len = null_test(str);

    printf("lenth = %d\n", len);

    return 0;        
}


 문자열 "abcde"가 메모리에 저장된 모습은 아래와 같다. 'a'가 저장된 주소를 1000번지라고 가정한다.


1000                     1001                       1002                    1003                       1004                    1005

a

b

c

d

e

\0

pstr+0                    pstr+1                    pstr+2                   pstr+3                   pstr+4                   pstr+5


 위와 같이 문자열은 마지막에 NULL로 끝난다.

'프로그래밍 > Daily Coding' 카테고리의 다른 글

데이터형 정의하기 (typedef)  (0) 2014.02.20
열거형 기본 형태  (0) 2014.02.19
공용체 사용  (0) 2014.02.10
구조체 사용  (0) 2014.02.05
포인터 이해하기  (0) 2014.02.04

+ Recent posts