Jump to content
  • Advertisement
Sign in to follow this  
thedustbustr

a stupid C struct compiler error (resolved)

This topic is 4100 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

gcc -ansi -pedantic -Wall -g -lm -o p3 p3.c
p3.c: In function 'list_insert':
p3.c:42: error: 'new' undeclared (first use in this function)
p3.c:42: error: (Each undeclared identifier is reported only once
p3.c:42: error: for each function it appears in.)
p3.c:43: error: expected expression before ')' token
make: *** [p3] Error 1

struct list_element { float element; struct list_element* next;};
typedef struct list_element node;

node* list_insert(node* node, float value){
    node* new;
    new=(node*)malloc(sizeof(node*));
    new->element=value;
    if (node) { /* needs to be atomic */
        new->next=node->next;
        node->next=new;
    } else { 
        new->next=NULL;
        node=new;
    }
    return node;
}
PS why isn't there a standard list like there is in C++? I freaking hate having to write my own little list in C land because my alg needs one.

Share this post


Link to post
Share on other sites
Advertisement
here it is in MSVC8

p3.c(42) : error C2065: 'new' : undeclared identifier
p3.c(42) : error C2296: '*' : illegal, left operand has type 'node *'
p3.c(43) : error C2059: syntax error : ')'
p3.c(44) : error C2223: left of '->element' must point to struct/union
p3.c(46) : error C2223: left of '->next' must point to struct/union
p3.c(47) : warning C4047: '=' : 'list_element *' differs in levels of indirection from 'int'
p3.c(49) : error C2223: left of '->next' must point to struct/union
p3.c(50) : warning C4047: '=' : 'node *' differs in levels of indirection from 'int'

Share this post


Link to post
Share on other sites
I know it's C, but you might want to try not using a word that's reserved in C++. i.e. not 'new' but use 'newNode'.

otherwise:


new=(node*)malloc(sizeof(node*));



should be

new=(node*)malloc(sizeof(node));



-me

Share this post


Link to post
Share on other sites
This:
Quote:
my error was trying to have a local variable 'node' when i already have a global struct 'node'. go me.
in addition to:
Quote:
PS why isn't there a standard list like there is in C++? I freaking hate having to write my own little list in C land because my alg needs one.
is why C is very much disliked.

C simply doesn't have some things. Like a useful standard library. Or namespaces.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!