0

Been trying to solve this problem for a while and I just can't fix it. I'm a student still learning C so I might be doing something completely wrong but I can't see it. I'm trying to declare two new struct pointers. I've tried initiating them to NULL but nothing. I'm stumped. I assume I'm trying to something I can't do but I don't know what it is. Thanks.

Here's my struct:

    struct node {
      int key;
      int count;
      struct node *left, *right;
    };
    typedef struct node node;

Here's where I'm trying to declare:

    node *insert_node(node *root, node *node) {

    node *pTraverse = root;
    node *currentParent = root;
    }

Here are the errors:

    assign6.c: In function 'insert_node':
    assign6.c:15: error: 'pTraverse' undeclared (first use in this function)
    assign6.c:15: error: (Each undeclared identifier is reported only once
    assign6.c:15: error: for each function it appears in.)
    assign6.c:16: error: 'currentParent' undeclared (first use in this function)
  • Not certain, but I think naming your `struct` and `typdef` identically may be your problem. Try changing it to `struct node_struct` and see what happens. – Politank-Z Apr 18 '15 at 01:00
  • 2
    `node *node` - um, interesting choice of parameter name. Use something else (*anything* else besides `root` is looking good). [See here](https://stackoverflow.com/questions/20274913/pointer-declared-in-beginning-of-scope-is-undeclared/20274941#20274941). – WhozCraig Apr 18 '15 at 01:12
  • Yes we have a winner thank you WhozCraig. Looking back now that was pretty silly naming. Thanks to the others as well. I've renamed my struct to node_struct for some clarity and everyone is working now. Thanks all! – user3038871 Apr 18 '15 at 01:18
  • I think @WhozCraig did not ask you to change the struct name, `struct node` is pretty good. It is rather the variable name you choose that clash with the type name. So rather than `insert_node(node *root, node *node)`, you need to use something like `insert_node(node *root, node *other)` – tivn Apr 18 '15 at 01:34
  • @tivn I didn't; but Politank-Z *did* (see first comment). – WhozCraig Apr 18 '15 at 01:35
  • @tivn @WhozCraig Yeah sorry for any confusion. But is the `typedef struct node node;` line good then? Or should I keep them different like `typedef struct node_struct node` (what I have now) in the future? – user3038871 Apr 18 '15 at 01:41
  • @WhozCraig Oh, you correct. user3038871, using same name for struct and the typedef is normal. It is not wrong. Refer to http://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions. – tivn Apr 18 '15 at 01:45
  • 1) do not typedef a struct definition. That just clutters the code, leads to mis-understandings, and clutters the compiler name space. 2) use meaningful parameter names. 3) do not use the same name for different meanings. then your code will be much clearer, much more understandable, confusion about name/meaning will be eliminated. You will thank your self many times over when (in 6 months or 6 years) that code needs to be maintained – user3629249 Apr 18 '15 at 05:37

0 Answers0