프로그램 사용/gcc2022. 3. 17. 12:05

weston 소스를 보는데 희한한(?) 문자열 선언이 보여서 확인

static const char * const connector_type_names[] = {
[DRM_MODE_CONNECTOR_Unknown]     = "Unknown",
[DRM_MODE_CONNECTOR_VGA]         = "VGA",
[DRM_MODE_CONNECTOR_DVII]        = "DVI-I",
[DRM_MODE_CONNECTOR_DVID]        = "DVI-D",
[DRM_MODE_CONNECTOR_DVIA]        = "DVI-A",
[DRM_MODE_CONNECTOR_Composite]   = "Composite",
[DRM_MODE_CONNECTOR_SVIDEO]      = "SVIDEO",
[DRM_MODE_CONNECTOR_LVDS]        = "LVDS",
[DRM_MODE_CONNECTOR_Component]   = "Component",
[DRM_MODE_CONNECTOR_9PinDIN]     = "DIN",
[DRM_MODE_CONNECTOR_DisplayPort] = "DP",
[DRM_MODE_CONNECTOR_HDMIA]       = "HDMI-A",
[DRM_MODE_CONNECTOR_HDMIB]       = "HDMI-B",
[DRM_MODE_CONNECTOR_TV]          = "TV",
[DRM_MODE_CONNECTOR_eDP]         = "eDP",
[DRM_MODE_CONNECTOR_VIRTUAL]     = "Virtual",
[DRM_MODE_CONNECTOR_DSI]         = "DSI",
[DRM_MODE_CONNECTOR_DPI]         = "DPI",
};

 

느낌은 알겠는데.. 도대체 어디서 정의된 문법이냐...

$ cat str.c
#include <stdio.h>

static const char * const connector_type_names[] = {
        [0]     = "Unknown",
        [1]         = "VGA",
        [2]        = "DVI-I",
        [3]        = "DVI-D",
        [4]        = "DVI-A",
        [5]   = "Composite",
        [6]      = "SVIDEO",
        [7]        = "LVDS",
        [8]   = "Component",
        [9]     = "DIN",
        [10] = "DP",
        [11]       = "HDMI-A",
        [12]       = "HDMI-B",
        [13]          = "TV",
        [14]         = "eDP",
        [15]     = "Virtual",
        [16]         = "DSI",
        [17]         = "DPI",
};

void main()
{
        for(int i = 0; i < 10; i++)
                printf("%s\n",connector_type_names[i]);
}

$ gcc str.c
$ ./a.out
Unknown
VGA
DVI-I
DVI-D
DVI-A
Composite
SVIDEO
LVDS
Component
DIN

 

 

$ cat str.c
#include <stdio.h>

static const char * const connector_type_names[] = {
        [2]     = "Unknown",
        [1]         = "VGA",
        [0]        = "DVI-I",
        [3]        = "DVI-D",
        [4]        = "DVI-A",
        [5]   = "Composite",
        [6]      = "SVIDEO",
        [7]        = "LVDS",
        [8]   = "Component",
        [9]     = "DIN",
        [10] = "DP",
        [11]       = "HDMI-A",
        [12]       = "HDMI-B",
        [13]          = "TV",
        [14]         = "eDP",
        [15]     = "Virtual",
        [16]         = "DSI",
        [17]         = "DPI",
};

void main()
{
        for(int i = 0; i < 10; i++)
                printf("%s\n",connector_type_names[i]);
}

$ gcc str.c
$ ./a.out
DVI-I
VGA
Unknown
DVI-D
DVI-A
Composite
SVIDEO
LVDS
Component
DIN

 

 

+

ISO C99, GNU C90 에서 지원하는 듯.

In ISO C99 you can give the elements in any order, specifying the array indices or structure field names they apply to, and GNU C allows this as an extension in C90 mode as well. This extension is not implemented in GNU C++.
To specify an array index, write ‘[index] =’ before the element value. For example,

int a[6] = { [4] = 29, [2] = 15 };

[링크 : https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html]

 

$ gcc -std=c89 str.c
str.c: In function ‘main’:
str.c:26:2: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
  for(int i = 0; i < 10; i++)
  ^~~
str.c:26:2: note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code

$ gcc -std=c90 str.c
str.c: In function ‘main’:
str.c:26:2: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
  for(int i = 0; i < 10; i++)
  ^~~
str.c:26:2: note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code

$ gcc -std=c99 str.c

'프로그램 사용 > gcc' 카테고리의 다른 글

gcc fstack-protector-strong  (0) 2022.12.06
gcc vectorization 실패  (0) 2022.06.02
static link  (0) 2022.02.07
구조체 타입과 변수명은 구분된다?  (0) 2021.11.18
gcc unsigned to signed upcast 테스트  (0) 2021.07.08
Posted by 구차니