A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Curry

  • one year ago

Can someone tell me if i'm writing this C code right?

  • This Question is Closed
  1. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I wrote the code inside the function "createList" everything else was given.

  2. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @wio @ganeshie8 @Luigi0210

  3. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @.Sam.

  4. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @freckles

  5. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    presumably you mean something like struct List *createList(int maxElements) { struct List *list = malloc(sizeof(struct List)); list->sortedList = malloc(sizeof(int) * maxElements); list->size = 0; list->maxSize = maxElements; return list; }

  6. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    no the function signature and all were given. we're only filling in the funcitons.

  7. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    so it has to be list* createList(int maxElement)

  8. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    surely you mean List not list? and that is using C++ not C syntax for struct types

  9. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    hmm, it says list*

  10. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    that's the code the professor gave.

  11. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    well regardless, the body should be correct

  12. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    aside from some explicit casting

  13. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    and why is it not c? the professor told us it was c.

  14. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    and what do you mean by explicit casting?

  15. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    C would require a typedef for you to use list or List to refer to struct List

  16. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Sorry there was a typedef, i just didn't ahve it in screen shot struct List *list = malloc(sizeof(struct List)); list->sortedList = malloc(sizeof(int) * maxElements);

  17. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    can you explain those above two lines?

  18. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    and why does what i did, not work?

  19. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    the first allocates a List object on the heap by requesting some block of memory big enough to fit a List (int *, int, int); the second line then allocates the internal 'array' used by the list for storing elements (enough space for maxElement number of int)

  20. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    wait how do you know it allocates an internal array?

  21. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    for one, sizeof(maxElements) is returning the sizeof the int variable which is probably 4 on your system; presumably you meant to allocate an array capable of holding maxElement number of int, in which case you meant sizeof(int) * maxElements. but then this shouldn't be the List object itself but its internal sortedList member which is presumably an internal array

  22. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    eh, it's the only tihng that seemed sensible; otherwise, where does it store list entries?

  23. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    so by doing sizeof(int)*maxelements, it creates 4bytes times the maxElements amount of space?

  24. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    and dont' you need list *sortedList = (list *)malloc(sizeof(maxElements)); ^^^^^ that in the first line?

  25. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    well, it requests a block of memory big enough to contain at least maxElements number of int (so if sizeof(int) = 4 then it allocates 4*maxElements number of bytes, yes)

  26. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    we used that in a previous class. but i wasn't quite sure why we did that.

  27. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    where we did (list *) and then malloc.

  28. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i don't rmemeber if the C compiler just warns you about the implicit pointer conversion but in case it actually spits out an error, yes, you would need an explicit cast there

  29. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ooo!

  30. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.