C: I can't change my struct but the error is: must use 'struct' tag. It's my teacher's fault? -


[c language] teacher give me , i'm not allowed change it:

struct nodescl {   int info;   struct nodescl* next; }; typedef nodescl* typescl; // problem here 

i have make program take 2 typescl equal length, , return new typescl called "min_list", minor element taken each "i" position. program:

typescl minelements(typescl l1, typescl l2) {     if(l1 == null)     {         return null;     }     typescl min_list = (typescl) malloc(sizeof(typescl));     if (l1->info <= l2->info)     {         min_list->info = l1->info;     }     else     {         min_list->info = l2->info;     }     min_list->next = minelements(l1->next, l2->next);     return min_list; } 

i got problem "must use 'struct' tag refer type 'nodescl'. real problem can't change struct , wasn't able find same problem (everybody allowed change struct solve problem). can fix problem without change struct? help. p.s. sorry... it's c language, not c++

if meant c++, need compile c++ compiler. if meant c, typedef needs be

typedef struct nodescl* typescl; 

either way, have problem following line:

typescl min_list = (typescl) malloc(sizeof(typescl)); 

sizeof (typescl) gives size of pointer type, not size of thing points to; you're not allocating enough memory instance of nodescl.

if meant c++, should written as

typescl min_list = new nodescl; 

if meant c, use

typescl min_list = malloc( sizeof *min_list ); // no cast, operand of sizeof 

gratuitous rant

your teacher promoting really bad habit hiding pointer type in typedef name. there's nothing name typescl indicates pointer-ness @ all, , it's confusing use 2 different names type instances , type pointers.

if want declare pointer nodescl instance, use nodescl *, so:

nodescl *min_list = new nodescl; //c++ struct nodescl *min_list = malloc( sizeof *min_list ); // c 

pointer semantics special, , you're not doing favors hiding them behind typedefs. only time it's reasonable if pointer type meant "opaque"; is, user of type never meant dereference directly, rather pass api dereferencing behind scenes.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -