Curry
  • Curry
Can someone tell me if i'm writing this C code right?
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
chestercat
  • chestercat
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
Curry
  • Curry
I wrote the code inside the function "createList" everything else was given.
Curry
  • Curry
@wio @ganeshie8 @Luigi0210
Curry
  • Curry
@.Sam.

Looking for something else?

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

More answers

Curry
  • Curry
@freckles
anonymous
  • anonymous
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; }
Curry
  • Curry
no the function signature and all were given. we're only filling in the funcitons.
Curry
  • Curry
so it has to be list* createList(int maxElement)
anonymous
  • anonymous
surely you mean List not list? and that is using C++ not C syntax for struct types
Curry
  • Curry
hmm, it says list*
Curry
  • Curry
that's the code the professor gave.
anonymous
  • anonymous
well regardless, the body should be correct
anonymous
  • anonymous
aside from some explicit casting
Curry
  • Curry
and why is it not c? the professor told us it was c.
Curry
  • Curry
and what do you mean by explicit casting?
anonymous
  • anonymous
C would require a typedef for you to use list or List to refer to struct List
Curry
  • Curry
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);
Curry
  • Curry
can you explain those above two lines?
Curry
  • Curry
and why does what i did, not work?
anonymous
  • anonymous
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)
Curry
  • Curry
wait how do you know it allocates an internal array?
anonymous
  • anonymous
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
anonymous
  • anonymous
eh, it's the only tihng that seemed sensible; otherwise, where does it store list entries?
Curry
  • Curry
so by doing sizeof(int)*maxelements, it creates 4bytes times the maxElements amount of space?
Curry
  • Curry
and dont' you need list *sortedList = (list *)malloc(sizeof(maxElements)); ^^^^^ that in the first line?
anonymous
  • anonymous
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)
Curry
  • Curry
we used that in a previous class. but i wasn't quite sure why we did that.
Curry
  • Curry
where we did (list *) and then malloc.
anonymous
  • anonymous
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
Curry
  • Curry
ooo!

Looking for something else?

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