[c언어] 구조체 안에 상수 사용이 안됩니다.

gnfpsclvkdl의 이미지

const int MAX_LIST_SIZE = 100;
 
typedef int element;
 
typedef struct {
 
    element list[MAX_LIST_SIZE];
 
    int length;
 
} ArrayList;
 
int main(void){...}

#define을 사용안하고 const를 사용해서 상수를 정의하려고 하는데, 위의 방식으로는 작동이 안되네요.
왜 그런지 아는분 계시면 답변 부탁드립니다.

pynoos의 이미지

작동이 안된다는것은 컴파일이 안된다는 것인가요? 보기에 문제 없어 보입니다만.
사용하는 컴파일러 버전이나 C표준이 어떻게 되시나요?

gnfpsclvkdl의 이미지

visual studio 2017 버전을 설치해서 사용 중입니다.

컴파일 시에 오류가 발생합니다.

E0028 식에 상수 값이 있어야 합니다. [9]
C2057 상수 식이 필요합니다. [9]
C2229 struct 'unnamed-tag'에 크기가 0인 잘못된 배열이 있습니다. [11]

이와 같은 3개의 오류가 발생합니다.

라인 [9] : element list[MAX_LIST_SIZE];
라인 [11] : int length;

사용한 헤더는 stdio.h, 파일 형식은 *.c 입니다. (cpp가 아닌)

karkayan의 이미지

C++에서 const int는 상수지만,
C에서는 상수 취급되지 않습니다. 이 경우 define 또는 enum 을 사용해야 합니다.

ymir의 이미지

const int MAX_LIST_SIZE = 100;

는 constant 를 정의한 게 아니라 MAX_LIST_SIZE 라는 변수를 선언한 것입니다.
const 는 단지 read only 를 뜻하는 qualifier 입니다.

array 를 선언할 때 변수를 쓰는 경우, 그 array 를 VLA (variable length array) type 이라고 하는데..
VLA 는 VM (variably modified) type 에 속합니다.

struct 나 union 에서는 VM type 을 쓸 수 없고..
static 이나 extern storage-class 를 가진 array 는 VLA type 으로 선언할 수 없습니다.

정리하자면 flexible length array 는 struct 나 union 에서는 사용할 수 없고..
전역 변수로도 쓸 수 없다는 뜻입니다.

#define 이나 enum 쓰셔야 합니다.

참고:
http://en.cppreference.com/w/c/language/array
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
- 6.7.2.1 Structure and union specifiers
- 6.7.5.2 Array declarators

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

shint의 이미지

- 배열 안되면. 포인터 사용하시면 되요. ㅇ_ㅇ;;
- cpp 로 컴파일 하시면 되구요.
- const 는 변경자이고. constant 는 상수'라고 합니다.

//
typedef int element[100]; 배열 사용 (이렇게도 가능하네요. 이상한 방법이지만... ㅇ_ㅇ??)
http://codepad.org/j2EXV0bu

//
C++ 컴파일
http://codepad.org/kGzOgYfI

//
Visual Studio 프로젝트 옵션 - C/C++ - 고급 - 컴파일 옵션 - C++ 코드로 컴파일 (/TP)

//
error: variably modified 'list' at file scope 검색

https://stackoverflow.com/questions/13645936/variably-modified-array-at-file-scope-in-c/13645995
However, this will generate an error if C99 mode is selected (-std=c99), it will only generate a warning (Wgnu-folding-constant) if -pedantic is selected.
그러나 C99 모드가 선택되면 (-std = c99) 오류가 발생하고 -pedantic이 선택되면 경고 (Wgnu 폴딩 상수) 만 생성됩니다.

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.