Typedef problem

This is a discussion on Typedef problem within the C Programming forums, part of the General Programming Boards category; I have a problem with 2 structures in a project of mine. My llNode structure has an ArrayQueue structure in ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    6

    Typedef problem

    I have a problem with 2 structures in a project of mine. My llNode structure has an ArrayQueue structure in it, but the ArrayQueue structure contains pointers to other llNodes. For me to use llNode pointers in my ArrayQueue, i used a typedef at the beginning, but it won't work because llNode isn't defined at that point.

    My code is below. Any help is greatly appreciated.

    Code:
    typedef llNode* arrEntry;
    
    typedef struct arrayqueue {
      //Structure for a queue of a locations' connections
      int front;
      int back;
      int size;
      arrEntry* array;
    } ArrayQueue;
    
    typedef struct llnode { 
      //Structure for a linked list node containing location info
      string name;
      int x, y;
      arrayqueue roads;
      struct llnode* next;
    } llNode;

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    typedef struct llnode* arrEntry;
    
    typedef struct arrayqueue {
      //Structure for a queue of a locations' connections
      int front;
      int back;
      int size;
      arrEntry* array;
    } ArrayQueue;
    
    typedef struct llnode { 
      //Structure for a linked list node containing location info
      string name;
      int x, y;
      arrayqueue roads;
      struct llnode* next;
    } llNode;
    ?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Code:
    typedef struct llnode* arrEntry;
    
    typedef struct arrayqueue {
      //Structure for a queue of a locations' connections
      int front;
      int back;
      int size;
      arrEntry* array;
    } ArrayQueue;
    
    typedef struct llnode { 
      //Structure for a linked list node containing location info
      char name[60];
      int x, y;
      ArrayQueue roads;
      struct llnode* next;
    } llNode;

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    Thanks, i made the change in the first typedef line, and it works. Because i didn't have struct there it didn't work.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    string? Hmmm, are we using C/C++ mix here?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    oh sorry, i had a typedef for string earlier in my code

    Code:
    typedef char* string;
    which brings up another question.... my scanf's had problems when i tried to scan strings into char*'s, but when i changed them to char [5], the problem disappeared. why's that?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Probably because your char * hasn't got any memory actually assigned to it. Perhaps you need to malloc some memory or some such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    It is typically a bad idea to typedef such things, such as char* because it's hard for any programmer besides you to know what the actual type is. A string can be char[] or char*, whatever works.
    I recommend you avoid typenames such as that, when they're obscure.
    Especially, I don't think it's a good idea to do away with *. So
    typedef char string;
    Would be better and you should do string* later.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    oh, that makes sense... so you can have:

    Code:
    char * hello = "Hello world";
    but you can't have

    Code:
    char * hello;
    scanf(" %s", &hello);

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Indeed. The second example is wrong on two occasions:
    hello is just a variable pointing at some random memory location, to memory you haven't allocated, so you'll most likely get a crash when running that code. Secondly, scanf expects a char*, but since you are passing the address of a pointer, essentially you are passing a char**. Remove the & before the pointer to make it better. Though the code is far from ok...

    And a note for the first code: it should be const char* hello = "Hello world", because the string literal is read-only and thus if you modify it you get an access violation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Correct.

    You can of course have something like:
    Code:
    char * hello;
    
    hello = malloc(500);
    scanf("%s", hello);
    You should also be aware that scanf isn't the best function to read user input, particularly strings, as if the user types in too much input, you end up with a buffer overflow. Although there are some ways to counteract that, I would rather recommend the funciton fgets() - it is much more robust.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Oh, right.
    More about scanf: Need help with altering info on a txt file
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. typedef problem
    By dr$brown in forum C Programming
    Replies: 6
    Last Post: 12-25-2004, 11:27 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 06:35 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21