Something with structs...

This is a discussion on Something with structs... within the C Programming forums, part of the General Programming Boards category; Code: stuct listNode{ char data; stuct listNode *nextPtr; }; typedef struct listNode ListNode; typedef ListNode *ListNodePtr; What does the underlined ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    124

    Something with structs...

    Code:
    stuct listNode{
    char data;
    stuct listNode *nextPtr;
    };
    
    typedef struct listNode ListNode;
    typedef ListNode *ListNodePtr;
    What does the underlined line do? Since typedef is used before, shuldn't that just be : ListNode *ListNodePtr; ? And what ListNodePtr is now declared to be?

    note that in main i have this:
    Code:
    ListNodePtr startPtr = NULL;
    Also, about a linked list my book says that the link pointer in the last node of a list is set to NULL to mark the end of the list. How can i do that? How can i say for example that the 5th struct is the last one and set *nextPtr to NULL?

    Thanks in advance.
    Loading.....
    ( Trying to be a good C Programmer )

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    The underlined code defines a new type. The type ListNodePtr is defined as the type which is a pointer to a ListNode type variable.

    So

    ListNodePtr startPtr;
    ListNode *startPtr;
    struct listNode *startPtr;

    are all the same.

    You can set the pointer to NULL by assigning the value NULL to it. Like this:

    listPtr->nextPtr = NULL;

    Where listPtr is a pointer which points to a node in your list.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Sorry for not understanding you but:

    typedef ListNode *ListNodePtr;
    That shouldn't just be:
    ListNode *ListNodePtr;
    ?

    And what about the 5th struct in the list that i said about before?

    Thanks again
    Loading.....
    ( Trying to be a good C Programmer )

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    What I meant was: if I used your definitions then I could declare the variable startPtr in the three ways I wrote in my former post.

    If the 5th node of your list is the last one, then the member nextPtr of that node would have the value NULL. Note that you have to use an extra pointer to point to nodes to the list. I used a pointer listPtr which points to nodes to the list. Use this pointer to point to the 5th node and set nextPtr to NULL.

    Note that usually this is not needed. Usually when creating a node, you would set nextPtr default to NULL. When adding a node to the list. The pointer nextPtr points to the new node and the new node its nextPtr is then NULL by default.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    I still don't understand....
    Can you post me an example which will answer my two above questions?
    Thanks.
    Loading.....
    ( Trying to be a good C Programmer )

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    When rereading your first post, I saw you used the word declaration. Note that with typedef you don't declare something, but you define types.

    Code:
    /* Here the structure listNode is defined. */
    stuct listNode{
    char data;
    stuct listNode *nextPtr;
    };
    
    /* Here a type ListNode is defined, which is the same as struct listNode. In other words, a variable of type ListNode is a structure struct listNode. */
    typedef struct listNode ListNode;
    
    /* Here a type ListNodePtr is defined. A variable of type ListNodePtr is a pointer to a variable of type ListNode. */
    typedef ListNode *ListNodePtr;
    So

    typedef ListNode *ListNodePtr;

    is not the same as

    ListNode *ListNodePtr;

    In the first, you define a new type. In the second you declare a pointer variable.

    Setting the link pointer to NULL, can be done with assigning NULL to it. I already showed how to do that. What is it that you don't understand?

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Aha!!!!
    I think i started to see a bit light in this dark labyrinth...

    So, instead of:

    typedef ListNode *ListNodePtr

    i could say:

    typedef struct listNode *ListNodePtr;

    Right?

    But how can a struct be pointed? and why should instead of just a Listnode, to have the ListNodePtr points to that struct ( = *ListNodePtr ) ?

    About setting it to Null:
    Is it right to say this: , when wanting to stop at the 5th struct?

    ListNode->nextPtr->nextPtr->nextPtr->nextPtr->nextPtr = NULL;

    ?(That was what i meant about code )
    Thanks in advance.
    Loading.....
    ( Trying to be a good C Programmer )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 09:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 03:46 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 02:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 06:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 01:38 PM

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