다음의 코드가 왜 오류가 없는지 궁금합니다.

vudghkzm의 이미지

typedef void* (*_t)(void*);
 
int main() {
    _t a;
 
    return 0;
}

왜 _t a; 라고 선언한 줄에서 컴파일 오류가 안 나는지가 궁금합니다.
(*_t)(void*) 를 void* 라고 선언했는데, 이것이 그냥 _t 타입으로 써도 된다는 뜻이 왜 되는건지 모르겟어요.
아시는분은 알려주세요.

doldori의 이미지

int i; 라는 선언문과 동일한 형태이니 당연히 정상적으로 컴파일됩니다.
첫줄에서 _t는 함수 포인터형임을 뜻합니다. 즉 void*를 인자로 받고
void*를 반환하는 함수를 가리키는 포인터형을 _t라는 이름으로 typedef 한 것이죠.
예를 들어

typedef void* (*_t)(void*);
 
void* f(void*);
 
int main() {
    _t a = f; // or _t a = &f;
    a(0);     // or (*a)(0). the same as f(0) 
    return 0;
}
IDNed의 이미지

앞에 typedef를 쓰고 그 뒤에 변수선언을 하면, 그 변수는 변수가 아니라 타입명이 됩니다.

예를 들어 int a;라는 것은 a를 선언하지만...

typedef int a;하면 a를 int 타입으로 만듭니다.

그 후 a some_val;하면 some_val은 int가 됩니다.

ssehoony의 이미지

typedef void* (*_t)(void*);
라는건 보통 흔히 보는
typedef struct NodeTag Node;
라는 식의
typedef [OldType] [NewType];
방식이 아니기 때문에 이상하게 느껴지실 겁니다.

typedef void* (*_t)(void*);
이런 선언법은 "void* 를 파라미터로하고 void* 를 리턴하는 함수 포인터"를 _t 선언하는
typedef 의 또다른 문법입니다.
typedef [리턴타입] (*[NewType]) ([파라미터리스트]);
이런형식이므로 위에 있는
typedef [OldType] [NewType];
의 문법으로 어떻게 해석하면 그렇게되나~ 라고 고민하지 마시고
그냥 함수포인터에 대한 typedef 는
typedef [리턴타입] (*[NewType]) ([파라미터리스트]);
이렇게 사용한다고 추가로 암기하시면 됩니다.

PS. typedef 로 배열도 정의할 수 있는데 이것 역시 함수포인터 정의 처럼 특이한 문법을 사용합니다.

cinsk의 이미지

죠커의 이미지

pointer to function(pointer to void) returning pointer to void

자세한 내용은 cinsk님의 C FAQ나 K&R의 The C Programming Language 혹은 전웅님의 서적들을 참고하세요.

- CN의 낙서장 / HanIRC:#CN

댓글 달기

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