C언어 typedef 문에 대해서 의견을 여쩌봅니다. ㅎ

nice7285의 이미지


안녕하세요. ㅎㅎ
typedef문을 볼때마다 궁금(?) 또는 의아하다고 생각하는 부분이 있어요.

아래와 같은 구문이 있을때,

typedef struct tagStack {
int array[STACK_MAX];
int top;
int bottom;
} STACK;

tagStack라는 구조체를 typedef문으로 STACK 이라는 형태로 정의를 하고 있는데요..

tagStack이라고 구조체 명을 정의해 주었는데, typedef를 통해서 STACK으로 또 다시 정의하는 이유가 무엇일까요?

struct Stack {
int array[STACK_MAX];
int top;
int bottom;
};

이렇게 정의하는게 더 낫지 않은가요??

익명 사용자의 이미지

typedef가 없으면 타입 이름을 적어줄 때에 struct Stack 이라고 적어주어야 합니다. 불편하지요.

helloc_의 이미지

예의 코드를 볼때
typedef [struct tagStack { ... }] STACK;

[] 안의 내용을 STACK으로 정의하여
STACK qna; 가 -> tagStack qna 로 변환 한다고 생각합니다.

highest170의 이미지

가장 직관적으로 잘 이해되게 말씀해주셨네요.

고맙습니다.

- 실력없는 자존심만큼 비참한것은 없다.

HDNua의 이미지

1. tag 테이블
C에서 struct, union, enum 키워드를 이용해 사용자 정의 형식(User Defined Type: 이하 UDT)을 만들면,
그 이름은 바로 형식이 되는 것이 아니라 태그(tag)가 되어 tag 테이블에 들어갑니다.
태그는 형식이 아니기 때문에 다음과 같은 구문도 가능합니다.

struct data { int num; };
struct data data; // 올바른 문장
typedef struct data data; // 그래서 이것도 가능한 문장입니다.

이 말은 아직 data는 int, char와 같은, '형식'이 아니라는 뜻입니다.
반드시 앞에 struct, union, enum을 붙여주어야 형식으로써 작용할 수 있게 되지요.
즉 현재 상황은 다음과 같습니다.
struct data { int num; };
struct data data1; // 올바른 문장
struct data data;  // 올바른 문장
data data2; // 컴파일 오류

즉 data 자체를 형식으로 사용하지는 못하고 항상 앞에 struct 키워드를 붙여주어야 하니 번거롭죠. 그런 이유입니다.

2. 참고: typedef를 이용하여 형식으로 재정의
typedef는 다음과 같이 사용한다고 알려져있습니다.

typedef typeA typeB; // typeB를 typeA로 재정의.
typedef int Integer;
Integer a, b; // int a, b;
typedef int* IntPtr;
IntPtr c, d; // int *a, *b;

그런데 사실, typedef 뒤에 들어가는 구문은 변수의 선언과 완전히 동일합니다.

int intValue; // 이것이 변수의 선언이면,
typedef int intType; // 이렇게 사용하면 됩니다.
typedef int *intPtr; // 포인터에도 마찬가지이고,
typedef int intArr[]; // 배열에도 마찬가집니다.
typedef int type1, type2; // 이러면 한꺼번에 type1, type2 형식이 모두 int로 정의됩니다.
typedef int type3, *type4; // type3는 int, type4는 int *로 정의됩니다.
 
int (*function_ptr)(); // 함수를 가리키는 포인터의 선언이 이와 같으므로,
typedef int (*function_ptr_type)(); // 함수 포인터 형식을 재정의할 때도 이런 식으로 합니다.
 
typedef int Int, *IntPointer, IntArray[], (*IntFunctionVoid)(), * (*IntPtrFunctionIntInt)(int, int);
// 각각의 형식이 무엇일지 생각해보는 것도 재밌겠습니다.

helloc_님께서 typedef typeA typeB; 부분의 것과 같은 설명을 해주셨기에 덧붙였습니다.

저는 이렇게 생각했습니다.

댓글 달기

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