Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

이런 Warning을 어떻게 수정하면 좋을까요?

2,462 views
Skip to first unread message

강명규

unread,
May 27, 2003, 12:43:13 AM5/27/03
to
2가지 경고가 발생하는데 정확한 이유가 무엇인지?
올바른 코딩방법을 추천해 주세요

warning: missing braces around initializer
warning: array subscript has type `char'

[kang@dbakorea ~/test]$ cat a.c
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <unistd.h>

typedef struct
{
char name [30];
char value [100];
} _AA;

_AA aa[] = {
{"kang" , NULL},
{"myung", NULL},
{"gyu" , NULL},
{NULL, NULL}
};


int main(void)
{
char i;

for( i=0; *aa[i].name != NULL; i++)
{
strcpy( aa[i].value , "kang");
printf("%s\n", aa[i].value);
}
return 1;
}

[kang@dbakorea ~/test]$ gcc -Wall -o a a.c
a.c:13: warning: missing braces around initializer
a.c:13: warning: (near initialization for `aa[0].value')
a.c: In function `main':
a.c:24: warning: array subscript has type `char'
a.c:26: warning: array subscript has type `char'
a.c:27: warning: array subscript has type `char'
[kang@dbakorea ~/test]$ ./a
kang
kang
kang
[kang@dbakorea ~/test]$


Chong-Dae Park

unread,
May 27, 2003, 3:31:43 AM5/27/03
to
"강명규" <ka...@dbakorea.pe.kr> wrote:
> typedef struct
> {
> char name [30];
> char value [100];
> } _AA;
>
> _AA aa[] = {
> {"kang" , NULL},
> {"myung", NULL},
> {"gyu" , NULL},
> {NULL, NULL}
> };

char name[30]에 NULL을 배정해서 생긴 에러 아닌가요?
char *name; 처럼 포인터로 바꾸거나, 값이 없는 경우에 대한 처리를
바꾸는 것이 옳을 듯 싶습니다.


--
박종대
-- ' C-language Edition
#define cdpark /* TC Lab, Div. of CS, Dept. of EECS, KAIST */
#include <signature.h> /* the Hitchhiker's Guide to the Internet?? */

지나가다가...

unread,
May 27, 2003, 6:17:21 AM5/27/03
to
> #include <string.h>
> #include <strings.h>
> #include <stdio.h>
> #include <unistd.h>
>
> typedef struct
> {
> char name [30];
> char value [100];
> } _AA;
>
> _AA aa[] = {
> {"kang" , NULL},
> {"myung", NULL},
> {"gyu" , NULL},
> {NULL, NULL}
> };
>
>
> int main(void)
> {
> char i;
>
> for( i=0; *aa[i].name != NULL; i++)
> {
> strcpy( aa[i].value , "kang");
> printf("%s\n", aa[i].value);
> }
> return 1;
> }
>

문법상 하자가 없는 것 같은데요..
다만 배열 첨자 사용하실때 정수형으로 사용하는게 좋을 것 같은데...

정확한 원인을 아시면 다시 한번 올려주세요..


dbakorea

unread,
May 27, 2003, 7:58:10 AM5/27/03
to
다음과 같이 수정했습니다.
소스의 변경을 줄이기 위해 _AA를 그대로 사용했는데 underscore('_')로
시작하거나 __로 시작하는
것들은 reserved되어 있다고 합니다. 확인은 못해봤지만, 이것이 올바른
방식이라면 그렇게 해야 겠네요
이제까지 _AA와 같이 사용했는데.. 흠..

#include <string.h>
#include <stdio.h>
#include <unistd.h>

typedef struct
{
unsigned char *name;
unsigned char value[100];
} _AA;

_AA aa[] = {
{"kang" , ""},
{"myung", ""},
{"gyu" , ""},
{NULL, ""}
};


int main(void)
{
int i;

for( i=0; aa[i].name != NULL; i++)


{
strcpy( aa[i].value , "kang");
printf("%s\n", aa[i].value);
}

return 0;
}

"강명규" <ka...@dbakorea.pe.kr> wrote in message
news:bauofh$1lh$1...@news1.kornet.net...

0 new messages