함수를 쉽게 호출하는 방법

익명 사용자의 이미지

일단 C언어를 사용하고 있습니다.
여러 함수들을 반복문을 사용해서 호출하고 싶어서
밑처럼 코드를 짜봤는데 역시나 실패했습니다.

저 컴파일 오류 난 부분처럼 제가 의도한 기능을 성공적으로 수행하는
다른 좋은 방법이 없을까요?

#include <stdio.h>
#define CALLER(x)   Func##x()
 
void Func1();
void Func2();
void Func3();
 
int main()
{
   int i;
 
   /* 이건 잘 되지만 너무 노가다 */
   CALLER(1);  // 실행 OK
   CALLER(2);  // 실행 OK
   CALLER(3);  // 실행 OK
 
   /* 원래 의도한 것 */
   for (i = 1; i <= 3; ++i) {
      CALLER(i);  // 컴파일 오류
   }
 
   return 0;
}
 
void Func1() {...}
void Func2() {...}
void Func3() {...}

제가 함수 여러 개를 쉽게 차례차례 호출하고 싶은데 방법을 모르겠습니다.
호출해야 할 함수가 한 두개면 문제가 아닌데 엄청 많아서.. 도움 주시면 정말 감사하겠습니다.

익명 사용자의 이미지

$ gcc -E test.c
void Func1();
void Func2();
void Func3();
 
int main()
{
   int i;
 
 
   Func1();
   Func2();
   Func3();
 
 
   for (i = 1; i <= 3; ++i) {
      Funci();
   }
 
   return 0;
}
 
void Func1() {}
void Func2() {}
void Func3() {}

gcc -E 명령을 실행시키면 전처리기만 실행합니다. 이제 감이 대강 오시나요?

익명 사용자의 이미지

함수포인터를 사용해 보세요.

void (*funcs[3])() = {Func1, Func2, Func3};
for (int i=0; i<3; i++) funcs[i]();
세벌의 이미지

함수포인터 쓰면 되고요, 함수 갯수가 가변적이라면 가변 인자를 쓰는 방법도 공부해 보셔요.
http://norux.me/19 참고.

bushi의 이미지

적절히 쓰면 꽤 유용한...

#include <stdio.h>
 
typedef void (*func_t)(void);
 
/* GCC */
#define __func __attribute__((section (".data.ro.my_funcs")))
#define register_func(x) func_t funcptr_##x __func = x
 
static void func1(void)
{
        printf("I'm %s\n", __func__);
}
 
static void func2(void)
{
        printf("I'm %s\n", __func__);
}
 
static void func3(void)
{
        printf("I'm %s\n", __func__);
}
 
static func_t func_start __func = NULL;
register_func(func1);
register_func(func2);
register_func(func3);
static func_t func_end __func = NULL;
 
int main(int argc, char **argv)
{
        func_t *func;
        for (func = &func_start + 1; func < &func_end; func++) {
                (*func)();
        }
        return 0;
}

댓글 달기

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