c 언어 ) define 매크로 사용하여 타입선언한 후 메서드 파라미터 사용 문의

syms의 이미지

c 코드에서 함수 원형에선 오류가 없는데 구현부에서 지역변수로 받으려고 이름을 지정하면
오류가 발생합니다. 어떻게 해야 될까요??

#define MAX_PERSON 10
#define MAX_NAME 20

#define d_nameArray char[MAX_PERSON][MAX_NAME]

// 아래는 오류가 없음

int input_name(d_nameArray);
void print_name(d_nameArray, int cnt);
int is_duplicated(d_nameArray, int current);

// error: expected ‘;’, ‘,’ or ‘)’ before ‘nameArray’ 오류 메시지

int input_name(d_nameArray nameArray)
{
// ....

return 0;

}

 의 이미지

int input_name(d_nameArray nameArray)
{
    // ...

=>

int input_name(char[10][20] nameArray)
{
    // ...

딱 봐도 안 될 것 같죠? 원래 아래와 같이 해야 맞는 거잖아요.

int input_name(char nameArray[10][20])
{
    // ...

반면에 함수 원형에서 파라미터 이름을 지정하지 않을 땐 그냥 아래와 같이 써도 무방하고,

int input_name(char [10][20]);

따라서 아래 코드엔 문제가 없는 겁니다.

int input_name(d_nameArray);

선언 대상의 앞과 뒤를 모두 감싸는 C언어의 선언 문법은 이런 경우에 꽤 번거롭죠. 단순히 텍스트 치환만 하는 #define만 가지고는 안 됩니다. 그럴 때 쓰라고 바로 typedef가 있는 거죠.

#define MAX_PERSON 10
#define MAX_NAME 20
 
// #define d_nameArray char[MAX_PERSON][MAX_NAME]
typedef char d_nameArray[MAX_PERSON][MAX_NAME];
 
// 아래는 오류가 없음
int input_name(d_nameArray);
void print_name(d_nameArray, int cnt);
int is_duplicated(d_nameArray, int current);
 
// 이제 이것도 오류가 없음
int input_name(d_nameArray nameArray)
{
    // ....
    return 0;
}

배열이나 함수 타입에 대해 typedef을 쓰는 법을 익혀두세요. C언어 계속 쓰다 보면 언젠간 분명히 요긴하게 써먹을 날이 올 겁니다.

댓글 달기

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