구조체 포인터를 type으로 선언하고, 그 변수로 구조체 멤버에 ?

indizarm의 이미지

typedef struct JKL* JKL_PTR;
typedef struct
{
    int i;
    char ch;
    JKL_PTR next;
}JKL;

void func()
{
    JKL_PTR tmp;
 
    tmp = (JKL_PTR)malloc(sizeof(JKL));

    tmp->i = 1;         // -----------> 이 방법 안됩니다. '->' == '*().'             
    tmp.ch = 'A';       // -----------> 역시 안됩니다. 당연히 tmp는 no JKL
    &tmp.next = NULL   // ------------> 더더욱 안됩니다. 거리는 멀어지고...
}

어떻게 해야 tmp를 이용해서 할당 받은 메모리에
있는 구조체 멤버에 접근할 수 있을까요?

참고로 '->' 방법은 dereferencing pointer to incomplete type
라는 에러 메세지를

'.' 과 '& .'은 request for member `변수명' in something not a structure or union
라는 에러 메세지를 띄웁니다.

세 가지 '삽질' 모두 터무니 없는 것이라는 것을 알지만
어떻게 해야 type으로 선언된 구조체 포인터를 통해
구조체 멤버에 접근할 수 있을까요?

achrom의 이미지

typedef struct JKL_
{ 
    int i; 
    char ch; 
    JKL_ *next; 
}JKL,* JKL_PTR;

tmp->i
tmp->ch
tmp->next

보통은 이렇게 쓰지 않나요?

indizarm의 이미지

예, 보통은 그렇죠. 그런데 왜 구조체 포인터를
type으로 선언했는지 모르겠군요. -_-;;

그냥 무의식적으로 tmp->i 하니까 에러가 나서
'컴파일러가 미쳤나' 했는데, 제가 뭔가 잘못된 것
같습니다. -_-;;;

저런식으로 구조체 포인터가 type으로 선언이
되었을 경우에 어떻게 해야 접근할 수 있을까요?

What a Cool Days!!!

lopad의 이미지

typedef struct JKL_ JKL;
typedef JKL* JKL_PTR;
struct JKL_ {
        int i;
        char ch;
        JKL_PTR next;
};

이런정도로 사용하지 않나요? struct 이름이 없는데 typedef struct JKL * JKL_PTR로 선언한게 이상해 보이는데요....[/code]
indizarm의 이미지

typedef struct JKL* JKL_PTR;
typedef struct
{
    int i;
    JKL_PTR next;
}JKL;

이렇게도 선언되는데요. 아무런 문제 없습니다.(변수 선언까지)

문제는 JKL_PTR 형태의 변수로 할당받은 구조체의 멤버에 접근
하기가 어렵다는/ 불가능하다는거죠... -_-;;;

아니면

typedef struct
{
    struct JKL* next;
}JKL;

이렇게도 됩니다.

What a Cool Days!!!

antibug의 이미지

전 그냥 이렇게 씁니다.

typedef struct JKL
{
    ...
    struct JKL *next ; 
} JKL ;

굳이 이름 바꿀 필요 있나요... ^^;

--------------------------------------
재미없는 일은 하지 말자는 인간 쓰레기.
-.-;

indizarm의 이미지

맞습니다. 굳이 이름 바꿀 필요없죠.

그런데 문제는 제가 헤더 파일을 만들었는데
그 파일에 구조체 포인터를 type으로 선언했다는
것과, 그 헤더 파일이 이미 다른 파일에서 include
되고 있는게 조금 걱정이죠. -_-;;

지금 생각해보니까 정신이 없었던 듯...
c++ 컴파일러 (VC++이든 gcc에서 호출되는 g++이든)
덕분에 c & c++의 hybrid code를 쓰던 습관때문에...

으아아~악

What a Cool Days!!!

댓글 달기

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