A community for students.
Here's the question you clicked on:
 0 viewing
Curry
 one year ago
Can someone tell me if i'm writing this C code right?
Curry
 one year ago
Can someone tell me if i'm writing this C code right?

This Question is Closed

Curry
 one year ago
Best ResponseYou've already chosen the best response.0I wrote the code inside the function "createList" everything else was given.

Curry
 one year ago
Best ResponseYou've already chosen the best response.0@wio @ganeshie8 @Luigi0210

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0presumably 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
 one year ago
Best ResponseYou've already chosen the best response.0no the function signature and all were given. we're only filling in the funcitons.

Curry
 one year ago
Best ResponseYou've already chosen the best response.0so it has to be list* createList(int maxElement)

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0surely you mean List not list? and that is using C++ not C syntax for struct types

Curry
 one year ago
Best ResponseYou've already chosen the best response.0that's the code the professor gave.

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0well regardless, the body should be correct

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0aside from some explicit casting

Curry
 one year ago
Best ResponseYou've already chosen the best response.0and why is it not c? the professor told us it was c.

Curry
 one year ago
Best ResponseYou've already chosen the best response.0and what do you mean by explicit casting?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0C would require a typedef for you to use list or List to refer to struct List

Curry
 one year ago
Best ResponseYou've already chosen the best response.0Sorry 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
 one year ago
Best ResponseYou've already chosen the best response.0can you explain those above two lines?

Curry
 one year ago
Best ResponseYou've already chosen the best response.0and why does what i did, not work?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0the 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
 one year ago
Best ResponseYou've already chosen the best response.0wait how do you know it allocates an internal array?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0for 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
 one year ago
Best ResponseYou've already chosen the best response.0eh, it's the only tihng that seemed sensible; otherwise, where does it store list entries?

Curry
 one year ago
Best ResponseYou've already chosen the best response.0so by doing sizeof(int)*maxelements, it creates 4bytes times the maxElements amount of space?

Curry
 one year ago
Best ResponseYou've already chosen the best response.0and dont' you need list *sortedList = (list *)malloc(sizeof(maxElements)); ^^^^^ that in the first line?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0well, 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
 one year ago
Best ResponseYou've already chosen the best response.0we used that in a previous class. but i wasn't quite sure why we did that.

Curry
 one year ago
Best ResponseYou've already chosen the best response.0where we did (list *) and then malloc.

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0i 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
Ask your own question
Sign UpFind more explanations on OpenStudy
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
 Engagement 19 Mad Hatter
 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.