static 기억부류를 갖는 대상체 초기화 질문입니다.

bureokjam의 이미지

#include <stdio.h>
 
double arr1[3];
int a = (int)"Hello";
int b = (int)arr1;
int c = (int)&a;
 
int main(void)
{
	/*
	static double arr2[3];
	static int i = (int)"World";
	static int j = (int)arr2;
	static int k = (int)&j;
	*/
	printf("%d %d %d\n", a, b, c);
	return 0;
}

1) 전역변수 a, b, c의 초기값으로 놓인 것들이 어떤 원리로 가능한 것인가요? 초기값이 상수식으로 취급 되는듯 싶어서 상수식만 올 수 있는 문맥에 위치시켜 보았더니 상수식은 아니라서 더욱 궁금하네요..

kcb1978의 이미지

static은 한번만 정의해서 사용 할수 있습니다

자료형을 배우고 변수를 배우고 대입 연산자를 배우고 연산자 순서를 배우면 도움이 많이 됩니다

raymundo의 이미지

질문의 요지는 글로벌 변수의 초기값을 상수식이 아니라 & + 다른변수이름 을 써서 수식으로 해도 되는 원리가 뭐냐는 것 아닌가요?

일단 리눅스 gcc 에서는 상수식이 아니라고 컴파일 에러가 나는군요.
다른 툴에서 컴파일에 성공했다면 그 툴이 따로 지원한 것 같네요. "Hello"의 주소는 컴파일타임에 결정할 수 있고, arr1과 a의 주소 역시 마찬가지로 컴파일타임에 정해지니까 그 뒤에 &a 를 써서 다른 변수를 초기화하는 것도 불가능한 일은 아닐테니까요.

좋은 하루 되세요!

ymir의 이미지

http://stackoverflow.com/questions/12750796/initializer-element-is-not-constant-in-c

constants expression 에 address constant 도 포함되어 있는데..
address constant 는 null pointer 는 물론, static storage duration 을 갖는 object 에 대한 pointer..
function 에 대한 포인터를 포함하고 있습니다.

$ cat gl.c
#include <stdio.h>
 
int foo(void)
{
        return 8;
}
 
double arr1[3];
int a = (int)"Hello";
int b = (int)arr1;
int c = (int)&a;
int d = (int)&a + 1;
int (*p_foo)(void) = foo;
 
int e = b + 1;
int f = foo();
 
int main(void)
{
        /*
        static double arr2[3];
        static int i = (int)"World";
        static int j = (int)arr2;
        static int k = (int)&j;
        */
        printf("%d %d %d\n", a, b, c);
        return 0;
}
 
$ gcc -g -W -Wall -ansi -pedantic gl.c
gl.c:15:9: error: initializer element is not constant
 int e = b + 1;
         ^
gl.c:16:9: error: initializer element is not constant
 int f = foo();
         ^
$ gcc -g -W -Wall -std=c99 -pedantic gl.c
gl.c:15:9: error: initializer element is not constant
 int e = b + 1;
         ^
gl.c:16:9: error: initializer element is not constant
 int f = foo();
         ^
$ gcc -v
(...)
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)

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