Thread: Is there a difference?

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    12

    Is there a difference?

    Hi everyone! I need your help to understand the situation below. Please be good to me..

    For example I have the given structure:

    Code:
    struct orders
    {
    int quantity; char foodname [50];
    } typedef struct orders ORDER; struct Table {
    int tableno; int priority; ORDER *orders;
    struct Table *next;
    } typedef struct Table TABLE
    Now I am required to implement a create function using singly linked list with a dummy node. I actually have several answers and I am not sure which one is correct.

    1st answer:
    Code:
    TABLE *create()
    {
    TABLE *newO;
    newO = (TABLE *)malloc(sizeof(TABLE)); newO->next = NULL; return newO;
    }
    2nd answer:
    Code:
    TABLE *create()
    {
    TABLE *newO; newO = (TABLE *)malloc(sizeof(TABLE)); newO->tableno = NULL; newO->priority = NULL; strcpy(newO->orders.foodname, " "); newO->orders.quantity = NULL; newO->next = NULL; return newO;
    }
    As you can see on my 2nd answer, i have set all the elements on the structure as NULL;

    3rd answer:
    Code:
    TABLE *create()
    {
    TABLE *newO; newO = (TABLE *)malloc(sizeof(TABLE)); if (newO) {
    newO->next = NULL; return newO;
    } else {
    return O;
    }
    }
    On my 3rd answer, I have an if statement to check if the pointer is null.

    4th answer:
    Code:
    TABLE *create()
    {
    TABLE *newO; newO = (TABLE *)malloc(sizeof(TABLE)); if (newO)
    {
    newO->tableno = NULL;
    newO->priority = NULL; strcpy(newO->orders.foodname, " "); newO->orders.quantity = NULL;
    newO->next = NULL; return newO;
    } else {
    return 0;
    }
    }
    Just same with 3rd answer but with the elements of the data structure set to NULL.

    Can you please tell me which one is the correct answer?
    Or are my answer all acceptable?

    Also if there is wrong with my answers, can you pinpoint which part is it?

    Looking forward to hear from everyone really soon.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The "correct answer" depends on what it answers. You haven't specified that.

    All of the "answers" are correct if the "correct answer" is "has at least one practical flaw". The difference between them is that they have different flaws, and some of them have multiple practical flaws.

    How about you describe what you think they are in each case?

    After all, this is clearly a homework and learning exercise, which means it is covered by this site's homework policy at this link.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    12
    hi grumpy. thanks for your response. We are actually just required to implement or have snippet of a "create" function of the orderlist using SLL with a dummy node. And that's just it. So I have created those answers.

    I just started studying data structures though and I am a bit confused about it. Take for example my 1st answer and 2nd answer. I am not sure if the elements of the structure will be automatically "null" if I just allocate a memory to it. That's why on my 2nd answer, I set all the elements to "null'.

    My 3rd answer would check if "newO" is null, so that's why I put this two lines "newO->next = NULL; return newO;" inside the if statement.

    My 4th answer actually was created just out of confusion.

    Please bear with me.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    malloc() does not initialise the memory it allocates in any way.

    It is considered bad practice to initialise scalars (float, int, etc) using NULL. NULL is intended for initialising pointers, not what the pointers point at (unless the pointee is itself a pointer).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by grumpy View Post
    malloc() does not initialise the memory it allocates in any way.

    It is considered bad practice to initialise scalars (float, int, etc) using NULL. NULL is intended for initialising pointers, not what the pointers point at (unless the pointee is itself a pointer).
    Which is strange because the OP returns 0 as a pointer yet initalises scalars with NULL. Perhaps there is a misunderstanding of a fundamental?

    OP: You're return O in your third example, not 0, as well.

  6. #6
    Registered User
    Join Date
    Jan 2014
    Posts
    12
    Hi Hodor. On my 3rd example I meant it to be return 0.. not return O...
    Last edited by begoodtome; 01-06-2014 at 08:08 PM. Reason: i forgot I can't edit the question..

  7. #7
    Registered User
    Join Date
    Jan 2014
    Posts
    12
    So, we'll just put it like this. If not considering the flaws of each answer, are my answers acceptable as the "answer"?

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    What do you mean by "acceptable"? You consider flaws to be acceptable?

    Is it acceptable for your function to leave things not done that can cause problems for the caller? The first and third answers can cause the caller to exhibit undefined behaviour (depending on what the caller subsequently does, of course). The second and fourth answers exhibit undefined behaviour all on their own.

    Is a compilation error acceptable? If so, then your third answer is acceptable.

    Is use of NULL to initialise scalars acceptable? [I would say not] If so, then your second and fourth answers are acceptable.



    More generally, however, you need to consider all the ways the code might malfunction or might cause other code to malfunction. While it is impossible to eliminate all malfunctions (after all, you can't control whether code you don't write causes your code to malfunction), you can do as much as possible to ensure your code doesn't cause malfunctions.

    EDIT: IMHO, describing software as a "little bit flawed" is like describing a woman as "a little bit pregnant".
    Last edited by grumpy; 01-06-2014 at 08:26 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Jan 2014
    Posts
    1
    I am going to point out some of the issues I see in the 4th answer:
    1. You need to allocate memory for the "orders" pointer before you copy to the "foodname" element and it has to be accessed like "orders->foodname" and not "orders.foodname"
    2. The same goes for accessing "orders->quantity"
    3. Avoid using NULL to initialize the int elements
    4. Inside the else, return NULL instead of 0

  10. #10
    Registered User
    Join Date
    Jan 2014
    Posts
    12
    Thank you for all your replies. I'll keep that in mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference??
    By aintgotnoname in forum C Programming
    Replies: 2
    Last Post: 11-25-2013, 10:54 PM
  2. HD difference?
    By yaya in forum Tech Board
    Replies: 28
    Last Post: 09-05-2009, 06:03 PM
  3. Difference Between C And C++
    By nathanpc in forum C++ Programming
    Replies: 13
    Last Post: 07-16-2009, 01:57 PM
  4. difference
    By srinu in forum Windows Programming
    Replies: 2
    Last Post: 02-13-2003, 04:19 PM
  5. What's the difference?
    By electrolove in forum C Programming
    Replies: 2
    Last Post: 02-12-2003, 02:31 AM

Tags for this Thread