#define 관련 질문입니다.

jsm427의 이미지


프로그램이 돌아가는 도중에 정의된 값을 수정해줘야 하는 경우가 발생했는데요.

예를 들어서

if(a == 1)
#define alpha a0
if(a == 0)
#define alpha a1

이런 식으로 코드를 구성하고 싶은건데.. #define은 전처리연산자이니깐 저렇게 작성하면 안되지요..?
어떤 방법으로 작성하면 alpha를 두 가지 경우에 대해서 설정할 수 있나요?

ifree의 이미지

상수를 정의하기 위해 #define 을 사용하는 것은 잊으라고 교과서에 나와 있네요.
const 변수로 정의하는게 바람직합니다.
정 저렇게 하고 싶으면 전역변수를 이용하는 수 밖에 없습니다만,
프로그램 구조를 좀 바꾸면 전역변수나 상수를 쓰지 않고 해결할 수 있을 것 같은데요.

jsm427의 이미지

감사합니다.

그런데 프로그램의 구조를 바꾸고 싶지만
너무 큰 프로그램이라.. 전부 바꾸는게 용의치 않아서요.

char 변수에 알파벳 값을 넣는 것과
#define 을 통해서 알파벳을 넣는 것이
같은 역할을 하나요?

예를 들어서 #define a0 alpha
라고 한 다음 a0를 사용하는 것과,
char a0 = alpha;
를 한 다음에 a0를 사용하는 것이 같은 동작을 하는지 알고 싶습니다.

그렇다면, 현재 헤더파일에 있는 #define으로 정의되어 있는 값들을 모두
const char 로 바꿔주어도 되는 것인지 알고 싶습니다.

감사합니다! ^^

ifree의 이미지

#define 은 타입을 따지지 않는 단순 치환이죠.
변수 a0가 프로그램 전체에서 char 로 쓰이고 있다면 const char 로 바꾸어도 무난합니다.

jsm427의 이미지

헤더파일에

#define a "alpha"

라고 되어있던 것을 변경하려고 하는 중인데
어떻게 하면 되지요..?

const로 지정할 수도 없는게 도중에 값을 변경시켜줘야 해서요..

extern을 사용해야 되는데..extern을 사용하면 초기값을 줄 수가 없고.. 허허.. ㅠㅠ

ymir의 이미지

#include <stdio.h>
 
static const char *a0 = "hello";
static const char *a1 = "world";
static const char *locl_alpha = NULL;
 
#define alpha get_alpha()
 
void set_alpha(int cond)
{
        if (cond == 0)
                locl_alpha = a0;
        else
                locl_alpha = a1;
}
 
const char *get_alpha(void)
{
        if (!locl_alpha)
                locl_alpha = a0;        /* by default */
 
        return locl_alpha;
}
 
int main(void)
{
        set_alpha(0);
        printf("alpha is %s\n", alpha);
 
        set_alpha(1);
        printf("alpha is %s\n", alpha);
 
        return 0;
}

이런식으로 사용할 수도 있습니다.
매크로는 그냥 예를 든 것으로..
저런식으로 함수를 매크로로 바꿔서 쓰다보면 만수무강에 지장이 생길수도 있습니다.. ㅎ~
매크로 빼고 locl_alpha 를 alpha 로 바꾸고, 앞에 static 빼면..
굳이 get_alpha() 안 써도, 아무데서나 바로 alpha 로 접근할 수 있겠죠..

되면 한다! / 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 』

visualplus의 이미지

혹시 이게 필요하신건지 모르겠습니다..

#define, #undef

댓글 달기

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