Thread: Linked list

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    11

    Linked list

    Hi..

    i would like to know why this code is working :

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    
    
    typedef struct
    {       
           
           int mem;
           struct node *next;
           
    }NODE;
    
    
    
    
    
    
    int main(int argc, char *argv[])
    {
        NODE n;
        
        NODE *pt;
        NODE *spt;
        
        pt = &n;
        
        
        int i;
        //create more nodes
        for(i = 0 ; i < 10 ; i++){
              
              pt -> mem = i + 1;
              pt -> next = malloc(sizeof(NODE));
              pt = pt -> next;
        }
        printf("Created\n");
       
       //display 
        pt = &n; 
        for(i = 0 ; i < 10 ; i++){
              
              printf("%d\n", pt -> mem );
              pt = pt -> next;
        }
        
        
        //free memory      
        pt = &n; 
        pt = pt -> next;
        for(i = 0 ; i < 10 ; i++){
              
              spt = pt -> next;
              free(pt);
              pt = spt -> next;
              
              printf("freed %d\n" , i);
        }
              
        
      
      getchar();
      return 0;
    }
    shouldn't the compiler complain about node (in the struct definition) not being declared?

    I am using dev-c++.. and when i tried to compile the code on codepad i got segmentation fault.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Actually, the definition of node within the typedef is valid. It is a forward declaration. Essentially, the declaration of node is telling the compiler there is a struct node type defined elsewhere.

    The code, however, would not give a segmentation fault, since the main() attempts to convert a pointer to "struct node" into a pointer to "struct NODE" in various places (lines 35, 53, and 55, on a quick skim). A "struct node" is not a "struct NODE" so the conversion is invalid. Your main function will not compile, and the compiler will complain bitterly about the lines I pointed out (if not others).

    So, unless your compiler itself is crashing, or "codepad" is crashing, I don't believe your claim that your program is yielding a segmentation fault.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    Quote Originally Posted by grumpy View Post
    The code, however, would not give a segmentation fault, since the main() attempts to convert a pointer to "struct node" into a pointer to "struct NODE" in various places (lines 35, 53, and 55, on a quick skim). A "struct node" is not a "struct NODE" so the conversion is invalid. Your main function will not compile, and the compiler will complain bitterly about the lines I pointed out (if not others).

    So, unless your compiler itself is crashing, or "codepad" is crashing, I don't believe your claim that your program is yielding a segmentation fault.
    The main function compiled. It is giving segmentation fault because of the last for loop, as it is accesing the memory beyond it allocated.

    Code:
    # cc -Wall cpb22.c
    cpb22.c: In function āmainā:
    cpb22.c:32: warning: assignment from incompatible pointer type
    cpb22.c:40: warning: assignment from incompatible pointer type
    cpb22.c:44: warning: assignment from incompatible pointer type
    cpb22.c:47: warning: assignment from incompatible pointer type
    #

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Code:
    typedef struct
    {      
            
           int mem;
           struct node *next;
            
    }NODE;
    This is a typedef of an anonymous structure. I would avoid anonymous structures in general... but it's important here because a list is supposed to reference itself through the next pointer. I don't think C gives you a method to declare pointers to anonymous structures. It is probably the ultimate source of warnings.

    >> The main function compiled
    It compiled with warnings.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Code:
    spt = pt -> next;
              free(pt);
              pt = spt -> next;
    shoudn't this be
    Code:
              spt = pt -> next;
              free(pt);
              pt = spt;
    it looks like you are skipping every other node.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 10:13 AM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM

Tags for this Thread