Array of Pointer to Function에 대한 질문입니다.

익명 사용자의 이미지

#define T_OPT 3
#define OPT 2

struct _a
{
int index;
void (* f_print) (void);
void (* print[OPT]) (void);
int (* do_func [OPT]) (char *);
} info[T_OPT];

void init_info(void);
void init_index(int, char **);

void f_s(void);
void p_s1(void);
int s1(char *);
void p_s2(void);
int s2(char *);

void f_m(void);
void p_m1(void);
int m1(char *);
void p_m2(void);
int m2(char *);

void f_q(void);
void p_q1(void);
int q1(char *);
void p_q2(void);
int q2(char *);

int print_info(int);

void main(int argc, char *argv[])
{
int i;

if( argc > 2)
{
puts("Usageipcs -[a][m|s|q]\n");
exit(0);
}
init_index(argc, argv);
init_info();

for(i=0; i print_info(i);
}

int print_info(int id)
{
int i;
char buf[10];

if(info[id].index == -1)
return(0);

info[id].f_print(); ===> 1
info[id].print[info[id].index](); ====>2
for(i=0; i<3; i++)
{
sprintf(buf, "%d%d%d%d%d", i, i, i, i, i);
info[id].do_func[info[id].index](buf); ====> 3
}
}

void init_index(int argc, char *argv[])
{
int i;

if( argc == 1 )
{
for(i=0; i < T_OPT; i++)
{
info[i].index = 0;
}
}
else if (!strcmp(argv[1], "-aq"))
{
info[0].index = 1;
info[1].index = -1;
info[2].index = -1;
}
else if (!strcmp(argv[1], "-am"))
{
info[0].index = -1;
info[1].index = 1;
info[2].index = -1;
}
else if (!strcmp(argv[1], "-as"))
{
info[0].index = -1;
info[1].index = -1;
info[2].index = 1;
}
else if (!strcmp(argv[1], "-a"))
{
info[0].index = 1;
info[1].index = 1;
info[2].index = 1;
}
else if (!strcmp(argv[1], "-q"))
{
info[0].index = 0;
info[1].index = -1;
info[2].index = -1;
}
else if (!strcmp(argv[1], "-m"))
{
info[0].index = -1;
info[1].index = 0;
info[2].index = -1;
}
else if (!strcmp(argv[1], "-s"))
{
info[0].index = -1;
info[1].index = -1;
info[2].index = 0;
}
else
{
puts("Usageipcs -[a][m|s|q]\n");
exit(0);
}
}

void init_info(void)
{

info[0].f_print = f_q;
info[0].print[0] = p_q1;
info[0].print[1] = p_q2;
info[0].do_func[0] = q1;
info[0].do_func[1] = q2;

info[1].f_print = f_m;
info[1].print[0] = p_m1;
info[1].print[1] = p_m2;
info[1].do_func[0] = m1;
info[1].do_func[1] = m2;

info[2].f_print = f_s;
info[2].print[0] = p_s1;
info[2].print[1] = p_s2;
info[2].do_func[0] = s1;
info[2].do_func[1] = s2;
}

void f_s(void)
{
printf("semaphore...\n");
}

void p_s1(void)
{
printf("OWNER GROUP....\n");
}

int s1(char *s)
{
printf("semapore info1|%s|\n", s);
}

void p_s2(void)
{
printf("MODE OWNER GROUP....\n");
}

int s2(char *s)
{
printf("semapore info2|%s|\n", s);
}

void f_m(void)
{
printf("shared memory...\n");
}

void p_m1(void)
{
printf("OWNER GROUP....\n");
}

int m1(char *s)
{
printf("shared memory info1|%s|\n", s);
}

void p_m2(void)
{
printf("MODE OWNER GROUP....\n");
}

int m2(char *s)
{
printf("shared memory info2|%s|\n", s);
}

void f_q(void)
{
printf("disk queue...\n");
}

void p_q1(void)
{
printf("OWNER GROUP....\n");
}

int q1(char *s)
{
printf("disk_que info1|%s|\n", s);
}

void p_q2(void)
{
printf("MODE OWNER GROUP....\n");
}

int q2(char *s)
{
printf("disk_que info2|%s|\n", s);
}

/************************************************************/

질문
info[id].(*f_print)(); ===> 1-1
info[id].(*print[info[id].index])(); ====>2-1
info[id].do_func[info[id].index](buf); ====> 3-1

1,2,3이 모두 가능은 하겠지만, 원칙대로 하면 모두 Pointer to Function
이지 Function이 아니므로 1-1, 2-1, 3-1이 맞을 것 같습니다.

그런데 1-1, 2-1, 3-1에서 모두 오류가 나는 것 같습니다.

어디가 잘못된 것인지 알려주실 분이 있으면 고맙겠습니다.

고수님들의 많은 조언을 부탁드립니다.

P.S 참고로 이 프로그램은 제가 제작한 disk_q의 정보를 보여줄 목적으로
ipcs의 일부 기능을 구현하는 초기 함수이고, 지금 현재로 별 무리없이 구
동이 됩니다.

익명 사용자의 이미지

질문
info[id].(*f_print)(); ===> 1-1
info[id].(*print[info[id].index])(); ====>2-1
info[id].do_func[info[id].index](buf); ====> 3-1

info[id].(*f_print)();
가 아니라
(*info[id].f_print)();
가 되어야 합니다.
두번째것도 마찬가지로 (*info[id].print[info[id].index])();
가 되어야 하죠..
세번째 것은 맞는 것 같은데요..
잠깐 착각하신듯..

익명 사용자의 이미지

정말 감사드립니다.

결합순위를 착각해서 Pointer to Function이 아닌 것을 Pointer to Func
tion으로 잘못 알았던 것 같습니다.

다시 한번 감사드립니다.

댓글 달기

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