function pointer 와 function pointer의 배열에 대한 질문 입니다.

study의 이미지

아래와 같은 code를 만들고 있는데요.
37번째 line 에서

InitStateMachine(&f, &transitions[0][0]);
를 쓰면 compile error가 없는데,

InitStateMachine(&f, transitions);
를 쓰면 아래와 같은 error가 나오네요.

move_box.c: In function ‘main’:
move_box.c:136:23: warning: passing argument 2 of ‘InitStateMachine’ from incompatible pointer type [-Wincompatible-pointer-types]
InitStateMachine(&f, transitions);
^~~~~~~~~~~
move_box.c:109:48: note: expected ‘void (**)(void)’ but argument is of type ‘void (* (*)[5])(void)’
void InitStateMachine(FSM *fsm, Event_Handler *handler)

이런 경우에는 어떻게 하는게 가장 좋은 방법일까요?

01 : ...
02 : typedef void (*Event_Handler)(void);
03 : ...
04 : Event_Handler transitions[_STATES_MAX][_EVENTS_MAX] = {
05 :   {OnL1_MoveTo_L2, Warning,        BoxManRest, GrabABox,    Warning},
06 :   {OnL2_MoveTo_L3, OnL2_MoveTo_L1, BoxManRest, Warning,     Warning},
07 :   {Warning,        OnL3_MoveTo_L2, BoxManRest, ReleaseABox, Warning}
08 :};
09 :...
10 :...
11 :void InitStateMachine(FSM *fsm, Event_Handler *handler)
12 :{
13 :#if 0
14 :   fsm->curState = _MOVEMAN_LEVEL_1;
15 :   fsm->handler = handler;
16 :#else
17 :   Event_Handler *h;
18 :
19 :   h = handler;
20 :#endif
21 :}
22 :
23 :int main(void)
24 :{
25 :   int32_t ch;
26 :   int32_t s;
27 :   static struct termios oldt, newt;
28 :   FSM f;
29 :   FSM_EVT e;
30 :   Event_Handler *handler;
31 :
32 :#if 0
33 :   printf("transitions = %p\n", (void *)transitions);
34 :   handler = &transitions[0][0];
35 :   printf("handler = %p\n", (void *)handler);
36 :#else
37 :   InitStateMachine(&f, &transitions[0][0]);
38 :#endif
39 :
40 :   return 0;
41 :}

익명 사용자의 이미지

transitions 은 타입이 2차원 배열이고 transitions[0][0] 는 타입이 Event_Handler 이니까 다른거죠.
그리고 Event_Handler 가 이미 함수 포인터 이니까 아래와같이 하는게 좋겠죠.

void InitStateMachine(FSM *fsm, Event_Handler handler)

만약에 InitStateMachine(&f, transitions); 와 같이 2 차원 배열을 인수로 받으려면 다음과같이 해야되요.
void InitStateMachine(FSM *fsm, Event_Handler (*array)[5])
{
    Event_Handler h;
    h = handler[1][2];
}

study의 이미지

감사합니다.
알려주신대로 하니까, compile error는 없어졌어요.
그런데, 제가 원래하려고 했던 것과는 조금 다르게 되어버렸네요.

여기서 원래 하고 싶었던 것은, 14번째 줄 ~ 15번째 줄에 있는 것처럼,
fsm->handler가 transition table을 가리키게 하고, fsm->handler를 통해서
각각의 event handler에 access하고 싶었거든요.

그러면, 어떻게하는 것이 좋을까요?
FSM은 아래처럼 선언해 놓았어요.

/* The structure of Finite-State-Machine */
typedef struct _fsm {
   uint32_t       curState;
   Event_Handler  *handler;
} FSM;
익명 사용자의 이미지

typedef struct _fsm {
   uint32_t       curState;
   Event_Handler (*handler)[5]
} FSM;
라스코니의 이미지

지금 되어 있는 코드가 원하시는 형태입니다.
단 void InitStateMachine(FSM *fsm, Event_Handler *handler)는 void InitStateMachine(FSM *fsm, Event_Handler handler)로 하시고요. 함수 포인터 자체가 값이기 때문에 값으로 넒겨주면 됩니다. transitions 배열을 초기화할 때 warning과 같이 함수명만 쓰는 것과 같죠.

InitStateMachine(&f, warning); 또는
InitStateMachine(&f, transitions[0][0]); 이런식으로 연결해서 쓰면 됩니다.

study의 이미지

친절한 답변 감사합니다.
제가 질문을 충분히 하지 못한 것 같네요.
제가 하고 싶은 것은, 아래 code에서 51, 52번째줄처럼, transitions에 정의되어 있는 event_handler
함수들을
FSM의 member handler를 이용해서 access하고 싶었어요.

아래에 code를 다시 정리해봤습니다.
물론 51, 52번째줄은 동작안하고 컴파일에러는 납니다.
제가 하고 했던 것을 설명하려고, 일단은 동작하지 않는 부분의 code를 포함시켰습니다.

01 :#include <stdio.h>
02 :#include <stdlib.h>
03 :#include <string.h>
04 :#include <stdint.h>
05 :
06 :void Event_Handler_1(void)
07 :{
08 :   printf("This is Event_Handler_1\n");
09 :}
10 :
11 :void Event_Handler_2(void)
12 :{
13 :   printf("This is Event_Handler_2\n");
14 :}
15 :
16 :void Event_Handler_3(void)
17 :{
18 :   printf("This is Event_Handler_3\n");
19 :}
20 :
21 :void Event_Handler_4(void)
22 :{
23 :   printf("This is Event_Handler_4\n");
24 :}
25 :
26 :void Event_Handler_5(void)
27 :{
28 :   printf("This is Event_Handler_5\n");
29 :}
30 :
31 :void Event_Handler_6(void)
32 :{
33 :   printf("This is Event_Handler_6\n");
34 :}
35 :
36 :typedef void (*Event_Handler)(void);
37 :typedef struct _fsm {
38 :   uint32_t    state;
39 :   Event_Handler  (*handler)[3];
40 :} FSM;
41 :
42 :int32_t main(void)
43 :{
44 :   FSM *f;
45 :   Event_Handler transitions[2][3] = {
46 :      {Event_Handler_1, Event_Handler_2, Event_Handler_3},
47 :      {Event_Handler_4, Event_Handler_5, Event_Handler_6}
48 :   };
49 :
50 :   transitions[0][0]();
51 :   f->handler = transitions;
52 :   f->handler[0][0]();
53 :   return 0;
54 :}
라스코니의 이미지

대충 어떤 것을 생각하고 계시는지 알겠군요. 아래 참고해서 수정해 보세요.

1) FSM *f ==> FSM f;

2) line 50 transitions[0][0](); ==> (*transitions[0][0])(); // 함수 포인터도 포인터입니다. 포인터가 가르키는 객체를 액세하려면 *ptr처럼 해 주어야 합니다. 이경우는 함수임으로 (*ptr)() 이런식으로 하면 됩니다.

3) line 51 f->handler = transitions; ==> f.handler = transitions;

4) line 52 f->handler[0][0](); ==> (*f.handler[0][0])();

study의 이미지

이제 거의 이해가 된 것 같습니다. ^^

댓글 달기

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