2008. 11. 16. 12:07

main 함수의 정확한 선언

int main(void);

int main(int argc, char *argv[]);
int main(int argc, char **argv);

/* argv 데이타와 주소를 변경하고 싶지 않을 때 */
int main(int argc, const char * const argv[]);
int main(int argc, const char * const * argv);



명령행 데이타를 argc, argv 형태로 변환하는 함수
#include <stdio.h>
#include <string.h>

/*! \brief 최대한 받아들이는 argument 갯수 */
#define MAX_ARG         10

/*! \brief argv용 버퍼의 최대 크기 */
#define MAX_CMDLEN      256


/*! \brief 입력된 명령행 버퍼를 argc, argv 형태로 변환하는 함수
 *
 * \param buf : 명령행으로 입력된 버퍼 포인터
 * \param argv [OUT] : 변환된 argv 형태의 포인터 배열
 *
 * \return argc 값 */
int parse_argument( const char *buf, char *argv[])
{
    int i,j;
    char ch;
    int len;
    int argc;
    int firstChar;

    /* 외부에서 포인터로 접근하기 위한 static 배열 */
    static char argv_buf[MAX_CMDLEN+1];

    len = strlen( buf);

    argc = 0;
    firstChar = 1;
    for( i=0,j=0; i<len; ++i) {
        ch = buf[i];

        /* 공백 문자다. 하나의 argument 끝 */
        if( ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') {
            if( firstChar == 0) {
                argv_buf[j] = '\0';

                ++j;
                ++argc;

                argv[argc] = NULL;

                firstChar = 1;

                if( j == MAX_CMDLEN || argc == MAX_ARG) {
                    break;
                }
            }
            continue;
        }

        if( firstChar == 1) {
            argv[argc] = &argv_buf[j];
            firstChar = 0;
        }
        argv_buf[j] = ch;
        ++j;
        if( j == MAX_CMDLEN) {
            argv_buf[j] = '\0';
            break;
        }
    }
    if( firstChar == 0) {
        argv_buf[j] = '\0';

        ++argc;

        argv[argc] = NULL;
    }

    return argc;
}


/*! \brief 테스트 함수 */
void CTestDlg::TestFunc(void)
{
    int i;

    const char *input = "  aaaaaaaaa abc 333 444 555 777 bbbbbbbb  ";

    int argc;
    char *argv[MAX_ARG];

    argc = parse_argument( input, argv);

    printf("argc : %d\n", argc);
    for( i=0; i<argc; i++) {
        printf("%d : %s\n", i, argv[i]);
    }

    printf("argv[argc] : %d\n", argv[argc]);
}




main 함수의 두번째 인자(argv)가 왜 const가 아닌가?
 * 다음 테스트 프로그램을 돌려보자.
int main(int argc, char *argv[])
{
    int i;

    printf("argc : %d\n", argc);
    for( i=0; i<argc, ++i) {
        printf("%d : %s\n", i, argv[i]);
    }

    strcpy( argv[0], "0123456789012345678901234567890123456789012345678901234567890123456789");

    printf("argc : %d\n", argc);
    for( i=0; i<argc, ++i) {
        printf("%d : %s\n", i, argv[i]);
    }
}

 * 이 테스트 프로그램을 위 parse_argument 함수에 적용해보면 똑 같은 결과가 나옴을 알 수 있다.
 * 즉, 내부 구현이 비슷함을 알 수 있다.


표준에서의 main 함수의 두번째 인자의 const 여부
 * C99에서의 언급
The parameters argc and argv and the strings pointed to by the argv array shall
be modifiable by the program, and retain their last-stored values between
program startup and program termination.

* C90 그리고 C++98 에서의 언급
   * C++은 C90에 기초하고, C90 표준을 subset로 가지고 있다.
In C90 it is mentioned:


"Program startup"

The function called at program startup is named main . The
implementation declares no prototype for this function. It can be
defined with no parameters:

int main(void) { /*...*/ }

or with two parameters (referred to here as argc and argv , though any
names may be used, as they are local to the function in which they are
declared):

int main(int argc, char *argv[]) { /*...*/ }


If they are defined, the parameters to the main function shall obey
the following constraints:

* The value of argc shall be nonnegative.

* argv[argc] shall be a null pointer.

* If the value of argc is greater than zero, the array members
argv[0] through argv[argc-1] inclusive shall contain pointers to
strings, which are given implementation-defined values by the host
environment prior to program startup. The intent is to supply to the
program information determined prior to program startup from elsewhere
in the hosted environment. If the host environment is not capable of
supplying strings with letters in both upper-case and lower-case, the
implementation shall ensure that the strings are received in
lower-case.

* If the value of argc is greater than zero, the string pointed to by
argv[0] represents the program name ;argv[0][0] shall be the null
character if the program name is not available from the host
environment. If the value of argc is greater than one, the strings
pointed to by argv[1] through argv[argc-1] represent the program
parameters .

* The parameters argc and argv and the strings pointed to by the argv
array shall be modifiable by the program, and retain their
last-stored values between program startup and program termination.