Something about typedef (data structures)(Linkedlist)

This is a discussion on Something about typedef (data structures)(Linkedlist) within the C Programming forums, part of the General Programming Boards category; Hello, There is a program which sorts the chars we input alphabetically by a linked list.At first we define structer. ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    151

    Question Something about typedef (data structures)(Linkedlist)

    Hello,
    There is a program which sorts the chars we input alphabetically by a linked list.At first we define structer.

    Code:
    struct linkedlist {
    stuct linkedlist *nextptr;
    char data ;
    } ;
    
    typedef linkedlist Linkedlist;
    Linkedlist *Linkedlistptr;
    /* I can not understand the definition with star. I figured out as it
      automatically defines struct variables as pointers. I dont know if I am true
    */
    Then there is a inserter function to which we send the startptr which is defined buy *Linkedlistptr definer. Here how we call the inserter function:

    >inserter(&startptr);

    and we instruct the inserter :
    >void inserter(Linkedlistptr *startptr);

    What I couldnt understand here is , isnt startptr is already a pointer , why are we sending it's adress?We definitely dont use startptr's adress in inserter function. So if I narrow down my questions as two :

    1- Linkedlist *linkedlistptr , provides linkedlistptr to automatically define struct pointers. Is it true ? ( Linkedlistptr startptr; So here startptr is a pointer right? )
    2-why do we need to send startptr's adress?

    Oh one more =>

    3-typedef linkedlist Linkedlist;
    Linkedlist *Linkedlistptr;

    Why did the book define Linekdlistptr by two steps . Couldnt we do

    >typedef linkedlist *Lişnkedlistptr ; ,

    ???
    Thank you for all responses.
    Last edited by Prelude; 09-27-2007 at 03:05 PM. Reason: Broke the long comment line.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I can not understand the definition with star.
    "T *obj" means that obj is a pointer to T, where T is any reasonably simple type.

    >1- Linkedlist *linkedlistptr , provides linkedlistptr to automatically define struct pointers. Is it true ?
    Not with the code you gave. Presumably you meant this:
    Code:
    typedef struct linkedlist Linkedlist;
    typedef Linkedlist *Linkedlistptr;
    This defines two type aliases. The first is an alias for the structure, and it's primarily there to save you from typing the required struct keyword. The second is an alias for a pointer to the structure.

    >2-why do we need to send startptr's adress?
    The inserter function doesn't return anything, so how do you update the list if the start pointer changes? The two ways to do that are to return a pointer to the new list start:
    Code:
    struct linkedlist *insert ( struct linkedlist *start );
    
    /* ... */
    
    start = insert ( start );
    And pass in a pointer to the start of the list:
    Code:
    void insert ( struct linkedlist **start );
    
    /* ... */
    
    insert ( &start );
    Without the typedefs you can more easily see that we're working with a pointer to a pointer. Adding a level of indirection means that any changes to *start will be reflected back in the calling code. Pointers are passed by value, which is why you can't simply pass a pointer and expect any changes to the pointer itself to stick.

    >Why did the book define Linekdlistptr by two steps
    Probably in anticipation of using just a bare structure object instead of always using pointers.
    My best code is written with the delete key.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your question is a bit vague and there seem to be several copying mistakes.

    Firstly, the last line is not a typedef but a declaration of a pointer to struct Linkedlist.

    The typedef is used for convenience. Without the typedef you'd have to delcare the pointer the following way:
    Code:
    struct linkedlist {
    struct linkedlist *nextptr;
    char data ;
    };
    
    struct linkedlist *Linkedlistptr;
    Since it might be inconvenient to add the struct keyword each time you want to delare a linkedlist pointer (instance), you can use the typedef to remove the need of it:
    Code:
    struct linkedlist {
    struct linkedlist *nextptr;
    char data ;
    };
    
    typedef struct linkedlist Linkedlist;
    Linkedlist *Linkedlistptr;
    What happens is that for the compiler Linkedlist translates to struct linkedlist, as in the previous snippet.

    May-be more usual, but equivalent way to create this typedef is
    Code:
    typedef struct linkedlist {
    struct linkedlist *nextptr;
    char data ;
    } Linkedlist;
    
    Linkedlist *Linkedlistptr;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Anon I did not copy them. So I did not make a mistake. My source was writing like that. (Deitel Deitel )

    I know what * do. I did not mean that by I do not understand the stars.

    Isnt startptr already a pointer?. So it will change mainly the startptr's pointing adress.I still dont get the point of transfering adress of a pointer. When we awkdakasld. Oh I got it during the writing. All right. I will write if anything else comes to my head. It is it for now. If you have any other point please make it. I need every details.

    Thank you.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Anon I did not copy them. So I did not make a mistake. My source was writing like that.
    Then your book sucks, and from what I've heard of your book, it doesn't suck that much. Let's take a look:

    >stuct linkedlist *nextptr;
    It's struct, not stuct.

    >typedef linkedlist Linkedlist;
    Illegal C. To specify a structure type, you must include the struct keyword.

    >void inserter(Linkedlistptr *startptr);
    With the definition of Linkedlistptr as an object, you can't use it as a type. This is illegal as well.

    The only possible corrections that make sense for your question are:
    Code:
    struct linkedlist {
    struct linkedlist *nextptr;
    char data ;
    } ;
    
    typedef struct linkedlist Linkedlist;
    typedef Linkedlist *Linkedlistptr;
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    151

    Post

    Quote Originally Posted by Prelude View Post
    >Anon I did not copy them. So I did not make a mistake. My source was writing like that.
    Then your book sucks, and from what I've heard of your book, it doesn't suck that much. Let's take a look:

    >stuct linkedlist *nextptr;
    It's struct, not stuct.

    >typedef linkedlist Linkedlist;
    Illegal C. To specify a structure type, you must include the struct keyword.

    >void inserter(Linkedlistptr *startptr);
    With the definition of Linkedlistptr as an object, you can't use it as a type. This is illegal as well.

    The only possible corrections that make sense for your question are:
    Code:
    struct linkedlist {
    struct linkedlist *nextptr;
    char data ;
    } ;
    
    typedef struct linkedlist Linkedlist;
    typedef Linkedlist *Linkedlistptr;
    Hey my friend , you must be upset of something. You are right , deitel does not suck. I did not get the point why you mentioned stuct , that is just a writing defect. Because I did not copy , I wrote while posting. I am sorry. And yes I forgot to put struct after the typedef. And also this is not the important issue as well. Sometimes I can not understand behaivors. I asked a couple of questions , you can be sure that if I had some issues with the other parts of codes or some confusions I would ask them too. That stuct , the forgeting of struct keyword , these were not that important anyway.
    But there is a part that you are definitely wrong. Void inserter(Listnodeptr *startptr) is not illegal. (See FAQ LOL ) I suggest you to think more detailed about that.
    In conclusion I want to say prelude. Take it easy man... Eh..

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    Well ozumsafa I happen to disagree. Omission or munging of the struct keyword is wrong unless you typedef'd the mistake away, like Prelude showed you. Your C compiler would agree with that. And in my opinion, this very issue of yours demonstates why typedefing structs carelessly is evil.

    >In conclusion I want to say prelude. Take it easy man... Eh..
    Stick to what Prelude said though. Most likely, you're just taking what was said the wrong way.

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Well , to be honest , I did take it a little bit wrong. Right , but he wrote aggresively as you can feel. Anyway , the code works on a couple of compilers I have. Also that is the way deitel&detiel used. I still cant get your point.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ozumsafa View Post
    Well , to be honest , I did take it a little bit wrong. Right , but he wrote aggresively as you can feel. Anyway , the code works on a couple of compilers I have. Also that is the way deitel&detiel used. I still cant get your point.
    The point is that if you are using typedef, you should use "typedef struct xx yy;", not "typedef xx yy;" - the latter is valid in C++, but not in C. If your compiler is C/C++ compiler it MAY accept the latter as an extension, but it's not good practice to use C++ things in C.

    --
    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.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Hey my friend , you must be upset of something.
    I don't get upset from trivial things like this.

    >That stuct , the forgeting of struct keyword , these were not that important anyway.
    I think they're very important, because the inconsistency you created with these problems makes your questions ambiguous.

    >I suggest you to think more detailed about that.
    I suggest you think more carefully before incorrectly correcting someone. Here are quotes from your original post:
    Code:
    typedef linkedlist Linkedlist;
    Linkedlist *Linkedlistptr;
    and we instruct the inserter :
    >void inserter(Linkedlistptr *startptr);
    The function declaration is a syntax error because Linkedlistptr is not a type, it's an object. Feel free to compile it yourself:
    Code:
    struct linkedlist {
      struct linkedlist *nextptr;
      char data;
    };
    
    typedef struct linkedlist Linkedlist;
    Linkedlist *Linkedlistptr;
    
    void inserter(Linkedlistptr *startptr);
    
    int main ( void )
    {
      return 0;
    }
    If what you wrote isn't what you meant to write, you have no excuse when someone corrects you.

    >Anyway , the code works on a couple of compilers I have.
    Then you're probably compiling as C++, which has subtly different semantics than C. I highly recommend figuring out how to compile strictly as C with your compiler if you intend to learn C.
    My best code is written with the delete key.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by ozumsafa View Post
    Well , to be honest , I did take it a little bit wrong. Right , but he wrote aggresively as you can feel. Anyway , the code works on a couple of compilers I have. Also that is the way deitel&detiel used. I still cant get your point.
    Hint: don't assume all programmers are male.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Okey , I give up , I will not insist on social compromise anymore. I was not talking about typedef struct matsp. And prelude , it works .Thank you all for generous responses.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I was not talking about typedef struct matsp.
    We help with the big picture, not just the exact questions that are asked. You can learn more that way, but if you're not interested in learning, we're not forcing you to hang around.
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Really , as a matter of opinion you are generally helping your self satisfaction. You really want me to learn , try to look at the person in front of you with the possibility that he does not be careful about the other matters from the main subject he wants to learn and also take into account that the possibility that some sources may be bigger than your whole picture and try to help me by focusing on my main question.Of course you can even not look at my posts , that is your decision . I am sorry , because my topics are often becoming this way. Probably It's my fault.I will try not to be uncareul while writing other parts of my post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 07:43 AM
  2. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 09:53 AM
  3. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  4. Replies: 4
    Last Post: 06-14-2005, 06:45 AM
  5. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 08:11 PM

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