Ask your own question, for FREE!
Computer Science 15 Online
OpenStudy (curry):

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

OpenStudy (curry):

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

OpenStudy (curry):

@wio @ganeshie8 @Luigi0210

OpenStudy (curry):

@.Sam.

OpenStudy (curry):

@freckles

OpenStudy (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; }

OpenStudy (curry):

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

OpenStudy (curry):

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

OpenStudy (anonymous):

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

OpenStudy (curry):

hmm, it says list*

OpenStudy (curry):

that's the code the professor gave.

OpenStudy (anonymous):

well regardless, the body should be correct

OpenStudy (anonymous):

aside from some explicit casting

OpenStudy (curry):

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

OpenStudy (curry):

and what do you mean by explicit casting?

OpenStudy (anonymous):

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

OpenStudy (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);

OpenStudy (curry):

can you explain those above two lines?

OpenStudy (curry):

and why does what i did, not work?

OpenStudy (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)

OpenStudy (curry):

wait how do you know it allocates an internal array?

OpenStudy (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

OpenStudy (anonymous):

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

OpenStudy (curry):

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

OpenStudy (curry):

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

OpenStudy (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)

OpenStudy (curry):

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

OpenStudy (curry):

where we did (list *) and then malloc.

OpenStudy (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

OpenStudy (curry):

ooo!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!