-1

I'm trying to do a simple program, adding a node to the end of the link list:

/*Insert Node at the end of a linked list 
  head pointer input could be NULL as well for empty list

Node is defined as 
  struct Node
  {
     int data;
     struct Node *next;
  }
*/

Node* Insert(Node *head,int data)
{

    if(head){
         Node *curr_node=head;

        while(curr_node->next)
            curr_node=curr_node->next;
    }

    Node *new_node=(Node *)calloc(1,sizeof(Node));
    new_node->data=data;
    new_node->next=NULL;

    if(head)
            curr_node->next=new_node;
    else
            head=new_node;

    return head;
}

/* the main function calls it*/

when I compile, I see the following error:

In function ‘Node* Insert(Node*, int)’: solution.cc:59:13: error: ‘curr_node’ was not declared in this scope curr_node->next=new_node

why does it say curr_node not declared, when it's actually declared right in the beginning. What am I missing here?

Miss J.
  • 351
  • 1
  • 5
  • 15
  • 3
    `curr_node` is only declared inside the `if`-block – Kninnug Oct 25 '16 at 16:42
  • 1
    And btw, there is no `Node`, only `struct Node`. Next time, try posting a *real*, [minimal, **complete**, verifiable example](https://stackoverflow.com/help/mcve) that reproduces your problem. – WhozCraig Oct 25 '16 at 16:43
  • 1
    The compiler did not say "not declared", it said *"in this scope"*. – Weather Vane Oct 25 '16 at 16:44
  • yes shouldn't it be taken into consideration only when head exists?? why would the program crib about it if there's no head i.e empty linked list ?? – Miss J. Oct 25 '16 at 16:45
  • Because in that case `curr_node` is not defined *at all*. Your question says "it's actually declared right in the beginning" but it is not. The conditional test is first. – Weather Vane Oct 25 '16 at 16:46
  • 1
    Or, just skip both `if` tests, use a pointer to pointer, and [cut the code size in half](http://pastebin.com/pzhAD8fa). – WhozCraig Oct 25 '16 at 16:48
  • 1
    just because nobody has mentioned it yet: Please don't cast the return of `malloc` & co – Elias Van Ootegem Oct 25 '16 at 16:50
  • @EliasVanOotegem I was going through your comment and referring back to the question:[Do I cast the result of malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc), where it mentions:void * is automatically and safely promoted to any other pointer type. However when I remove the type conversion, I see a compilation issue: invalid conversion from ‘void*’ to ‘Node* – Miss J. Oct 25 '16 at 17:04
  • Okay I figured it out , I was using a an editor set to c++ while doing this, and in c++ it's necessary to cast else it would throw an error – Miss J. Oct 25 '16 at 17:17
  • Curious:, why code `new_node=(Node *)calloc(...);` instead of `new_node=calloc(...);` IOWs, what lesson/instructor suggested code cast the result of `malloc()`? – chux - Reinstate Monica Oct 25 '16 at 17:34

1 Answers1

2

A variable declared in a function definition has a scope which extends only to the innermost {} braces. So your variable curr_node is no longer valid after the first if block.

To fix this, declare your variable outside the if block:

Node* Insert(Node *head,int data)
{
    Node *curr_node = NULL;
    if(head){
        curr_node=head;

        while(curr_node->next)
            curr_node=curr_node->next;
    }
aschepler
  • 70,891
  • 9
  • 107
  • 161
  • ooo gotcha!! I was under the impression that it's scope existed until the end of the function( never realized it was until the braces). Thanks!! :) – Miss J. Oct 25 '16 at 16:48