Thread: issue with linked list !!IMPORTANT

  1. #1
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41

    Question issue with linked list !!IMPORTANT

    Actually i am trying to learn data structures and came to linked list ,i tried to make a program and according to my knowledge its correct as the compiler(code::blocks) is showing 0 error 0 warnings, but still when i run my compiled program it crashes and stops execution don't kno why? pls help in the matter
    THE CODE IS:
    Code:
    #include<stdio.h>
    #include<strings.h>
    #include<stdlib.h>
    #include<malloc.h>
    
    
    struct node
    {
        int data;
        struct node *next;
    };
    
    
    typedef struct node link;
    
    
    link create_link(link *r);
    
    
    main()
    {
        link *trys,*k;
        int i,j,p;
    
    
        k=trys;
    
    
        printf("Enter the Number  data\n");
        scanf("%d",trys->data);
        trys->next=NULL;
        p: ;
        printf("\nDo you want to add more numbers?(Y=1/N=0)");
        scanf("%d",&i);
        if(i==1)
        {
            for(j=0;trys->next!=NULL;j++)
                    trys=trys->next;
            create_link(trys);
            {
                trys=k;
                for(j=0;trys->next!=NULL;j++)
                    trys=trys->next;
                printf("\nEnter the next Number data\n");
                scanf("%d",trys->data);
            }
            trys=k;
            goto p;
        }
        else
        {
            printf("\nNumber dat entered by you is:\n");
            for(j=0;trys->next!=NULL;j++)
            {
                printf("%d\n",trys->data);
            }
        }
    }
    
    
    link create_link(link *r)
    {
        link *addon;
    
    
        addon=malloc(sizeof(link));
        r->next=addon;
        addon->next=NULL;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    When I compile your code using my IDE (Code::Blocks) I get the following errors and warnings. Maybe you should increase the number of warnings being generated.

    main.c|20|error: return type defaults to ‘int’|
    main.c||In function ‘main’:|
    main.c|30|warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]|
    main.c|45|warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]|
    main.c|23|warning: unused variable ‘p’ [-Wunused-variable]|
    main.c||In function ‘create_link’:|
    main.c|69|warning: control reaches end of non-void function [-Wreturn-type]|
    ||=== Build finished: 1 errors, 4 warnings ===|
    Also you need to stop using goto, investigate one of the other more acceptable loop constructs.

    Jim

  3. #3
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41

    Question issue with linked list !!IMPORTANT

    Actually i am trying to learn data structures and came to linked list ,i tried to make a program and according to my knowledge its correct as the compiler(code::blocks) is showing 0 error 0 warnings, but still when i run my compiled program it crashes and stops execution don't kno why? pls help in the matter
    THE CODE IS:
    Code:
    
    
    Code:
    #include<stdio.h>
    #include<strings.h>
    #include<stdlib.h>
    #include<malloc.h>
    
    
    struct node
    {
        int data;
        struct node *next;
    };
    
    
    typedef struct node link;
    
    
    link create_link(link *r);
    
    
    main()
    {
        link *trys,*k;
        int i,j,p;
    
    
        k=trys;
    
    
        printf("Enter the Number  data\n");
        scanf("%d",trys->data);
        trys->next=NULL;
        p: ;
        printf("\nDo you want to add more numbers?(Y=1/N=0)");
        scanf("%d",&i);
        if(i==1)
        {
            for(j=0;trys->next!=NULL;j++)
                    trys=trys->next;
            create_link(trys);
            {
                trys=k;
                for(j=0;trys->next!=NULL;j++)
                    trys=trys->next;
                printf("\nEnter the next Number data\n");
                scanf("%d",trys->data);
            }
            trys=k;
            goto p;
        }
        else
        {
            printf("\nNumber dat entered by you is:\n");
            for(j=0;trys->next!=NULL;j++)
            {
                printf("%d\n",trys->data);
            }
        }
    }
    
    
    link create_link(link *r)
    {
        link *addon;
    
    
        addon=malloc(sizeof(link));
        r->next=addon;
        addon->next=NULL;
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. This is C not C++
    2. Line 30 - your tris pointer is not initialized so you cannot access its members
    3. Same line - scanf needs pointer to int variable to store value in it
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Duplicate from another thread in C++ forum. Hopefully it will be merged soon
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    @jim can please tell me what the error means and how to send a struct data for scanning? bcz in the book they were scanning like dis only.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    @vart how to send the struct data then?

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Amitesh93 View Post
    @vart how to send the struct data then?
    How do you get a pointer to variable?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    p->data

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    please tell me what the error means
    Which error message don't you understand, there's five separate errors?

    Jim

  11. #11
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    @vart are you asking me how to declare a pointer? or how to send a pointer

  12. #12
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    @jim error is one and all the rest warnings also can't get them.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First they all need to be considered errors. Those "warnings" are errors in disguise and are probably part of the problem.

    So you don't understand this error?
    main.c|20|error: return type defaults to ‘int’|
    You really need to start learning to read the error messages your compiler should be generating. This is telling you that you didn't provide a return type for the function main(). In C++, where you posted this C program, main() must be defined to return an int and you should return an int from this function. Since this is really a C program main should be defined to return an int and you should return an int from this function. In fact the current C standard also requires main to return an int.
    Code:
    int main()
    {
    
       return 0;
    }
    The rest of the "error" messages are also self-explanatory if you understand how to properly use the scanf() function. I suggest you review your documentation for that standard function.

    Jim

  14. #14
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    @gim in code::blocks are u using gnu/gcc compiler? how did u increased the errors and warnings as i could only increase the errors.
    rest i work on linux at school and there return type mentioning is not necessory .thats y i never came across mentioning a return type .
    plus what does this mean : main.c|69|warning: control reaches end of non-void function [-Wreturn-type]|

  15. #15
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    guys i removed [code]scanf("%d",trys->data);[/code] with
    Code:
    scanf("%d",&num);
    
        trys->data=num;
    still program is crashing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list issue
    By Hayat Qyrt in forum C Programming
    Replies: 16
    Last Post: 10-27-2012, 05:40 AM
  2. linked list issue
    By roaan in forum C Programming
    Replies: 6
    Last Post: 09-01-2009, 01:21 AM
  3. Doubly Linked List Deletion Issue
    By josephjah in forum C++ Programming
    Replies: 22
    Last Post: 07-22-2007, 03:00 PM
  4. Linked list copy constructor issue
    By Craptastic! in forum C++ Programming
    Replies: 1
    Last Post: 08-03-2003, 08:30 PM

Tags for this Thread