리눅스 C 문법 질문

rider의 이미지

아래와 같은 코드가 있습니다.

typedef struct ptentry ptentry;

struct ptentry {
char name[16];
unsigned start;
unsigned length;
unsigned flags;
};

ptentry PTABLE[] = {
{
.start = 285,
.length = 4,
.name = "usd",
},
{
.start = 289,
.length = 44,
.name = "pkg",
},
...;
};

for(int n=0; PTABLE[n].name[0]; n++) {
...;
}


위에서 보면 ptentry 라는 구조체를 PTABLE 배열로 선언하여 초기화? 해주고 있습니다.
초기화가 맞는지도 의문 입니다.

.start = 285, .length = 4, .name = "usd", <<=== 이게 도대체 무슨 표현인지요? .(dot) 을 이런식으로 사용해도 되는건지..

name 에 스트링값을 직접 할당해 주고 있는데 어찌 이런것이 가능한지 모르겠습니다.

또한 for 문을 보면 PTABLE 배열을 돌면서 name[0] 을 체크 하는데 name[0] 을 체크한다는것이 name 에 값이 있는지 없는지만을 보려는 의도 일까요?


처음 접해보는 리눅스 환경이라 간단한것들도 잘 안보이고 그냥 괜히 더 혼란스럽네요.
부디 저의 무지를 일깨워 주시기 바라며 상세한 설명 부탁드리겠습니다.
감사합니다.











mithrandir의 이미지

리눅스 C 문법이라고 하셨지만, 리눅스 C라는게 따로 존재하진 않습니다.
.fieldname 은 C99표준이라고 하는군요.

http://zone.ni.com/devzone/cda/tut/p/id/8764#toc4

Quote:

ANSI C requires that an initializer list for an array, struct, or union, begin with the first member of the aggregate, and proceed on in sequential order, without skipping any members. C99 provides a special “designator” syntax that allows the initialization of specific array, struct, or union members.

For structs or unions, to initialize a member x, use the designator .x= followed by the initial value:

name[0]를 본다는건 이게 empty string인지 아닌지 보는거죠. name[0]가 0이라면 길이가 0인 empty string입니다.

언제나 삽질 - http://tisphie.net/typo/
프로그래밍 언어 개발 - http://langdev.net

언제나 삽질 - http://tisphie.net/typo/
프로그래밍 언어 개발 - http://langdev.net

rgbi3307의 이미지

#include <stdio.h>
 
typedef struct ptentry ptentry;
 
struct ptentry {
        char name[16];
        unsigned start;
        unsigned length;
        unsigned flags;
};
 
ptentry PTABLE[] = {
        {
                .name= "usd", .start=285, .length=4
        },
        {
                .start=289, .length=44, .name="pkg" 
        },
        {
                .flags=3, .start=200, .name="jjj"
        }
};
 
main ()
{
        int n;
        for(n=0; PTABLE[n].name[0]; n++) {
                printf("name=%s, flags=%d\n", PTABLE[n].name, PTABLE[n].flags);
        }
}

아래와 같이 실행되는 군요.

$ cc -o ch0699 ch0699_temp01.c
$ ./ch0699
name=usd, flags=0
name=pkg, flags=0
name=jjj, flags=3
name=, flags=4
name=, flags=76
name=
, flags=2
name=, flags=134513176
name=, flags=1879048191
name=, flags=0

실행결과로 볼때, 리눅스에서 C Compiler는 구조체 멤버변수를 초기화 할때 designator(.x=)를
사용할 수 있는것 같습니다. 그리고, for 루프의 논리식 PTABLE[n].name[0]에서
false(0 혹은 널)가 아닌 true값이 좀더 있는듯 하네요. 초기화 될때 garbage값이 들어가는듯...

From:
*알지비 (메일: rgbi3307(at)nate.com)
*학창시절 마이크로마우스를 만들었고, 10년동안 IT관련 개발자로 일하고 있음.
*틈틈히 커널연구회(http://www.kernel.bz/) 내용물들을 만들고 있음.
*((공부해서 남을 주려면 남보다 더많이 연구해야함.))

From:
*알지비 (메일: rgbi3307(at)nate.com)
*커널연구회(http://www.kernel.bz/) 내용물들을 만들고 있음.
*((공부해서 남을 주려면 남보다 더많이 연구해야함.))

ymir의 이미지

해당 for 문은 PTABLE 의 entry 중 적어도 하나 이상은 반드시 name 이 empty 인 경우에만 유효합니다.
n 이 PTABLE 의 최대 index 를 초과하더라도 이를 검출할 수 없기 때문에 오류가 발생한 겁니다.
출력 결과의 네번째 줄 부터는 범위 밖의 메모리에 접근하여 찍은 값입니다.

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.