Please help me with Linklist problem

This is a discussion on Please help me with Linklist problem within the C Programming forums, part of the General Programming Boards category; I write a link list program of three functions 1. "ref.h" 2. "main.c" 3. "create.c" "create.c" accepts parameters from main.c ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    Smile Please help me with Linklist problem

    I write a link list program of three functions 1. "ref.h" 2. "main.c" 3. "create.c"
    "create.c" accepts parameters from main.c and add an new item to the link list
    I use pointer to pass the parameters to create.c, but I found my "front" variable in "create.c" is always NULL even though I have assigned new to it in the first item to be added.
    Could anyone help me to find why? I have stuck in this question for hours =.=
    Thank you very much!!

    ps. I run my program in Linux environment

    In ref.h
    Code:
    #include <stdio.h>
    struct data{
            int postcode;
            struct data *next;
    };
    In main.c
    Code:
    #include "ref.h"
    void main()
    {
    
            struct data *front=NULL;
            struct data *end=NULL;
    
            int a;
            while(1)
            {
                    printf("Add item to link list:\n");
                    printf("Enter 1 to add link list\n");
                    scanf("%d",&a);
                    switch(a)
                    {
                            case 1:{                        
                                        create(front,end);
                                        break;
                                    }               
                            default:
                                    exit(0);
                    }
            }
    }
    In create.c
    Code:
    #include "ref.h"
    
    void create(struct data *front,struct data *end)
    {
            struct data *new=(struct data *)malloc(sizeof(struct data));
            printf("Please enter the postcode:\n");
            int i;
            scanf("%d",&i);
            new->postcode=i;
            new->next=NULL;
            if(front==NULL)               each time the create function only runs in if 
            {
                    front=new;
                    end=new;
                    printf("First create!\n");
            }
            else
            {
                    end->next=new;
                    end=new;
                    printf("In create!\n");
            }
    
    }
    Last edited by ericwu; 05-13-2009 at 06:59 AM.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    I think you have to declare struct data *front and struct data *end outside of main (or with the EXTERN modifier) andin a header used by create.c. That way, they can be used as globally shared variables.

    Or you could try putting all the code into one source file to see if that works.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2
    The program works well when I consolidate the three fucntions together.
    But I really don't know why it couldn't work when I separate parts....sad

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ericwu View Post
    The program works well when I consolidate the three fucntions together.
    But I really don't know why it couldn't work when I separate parts....sad
    You didn't include enough information on how you have linked the two .c files. In My Opinion, you should be doing all of this with one file anyway.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    hi ericwu, here i modify your create function and also in main() to work correctly

    inside main() call...as

    Code:
    create(&front,&end);

    Code:
    void create(struct data **front,struct data **end)
    {
            struct data *new=(struct data *)malloc(sizeof(struct data));
            printf("Please enter the postcode:\n");
            int i;
            scanf("%d",&i);
            new->postcode=i;
            new->next=NULL;
            if(*front==NULL)               //each time the create function only runs in if 
            {
                    *front=new;
                    *end=new;
                    printf("First create! %p\n", front);
            }
            else
            {
                    (*end)->next=new;
                    *end=new;
                    printf("In create!\n");
            }
    
    }
    i hope you might have got answer..
    since you were telling that.. when you put in one file it is working.. i assume you made those front & end as global.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    1

    Question finding problems to solve the problem below

    writing a program that can input numbers and and print them in a rerve order.this problem is giving me troubles pliz me

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by inkonsinathi View Post
    writing a program that can input numbers and and print them in a rerve order.this problem is giving me troubles pliz me
    Do you mean the reverse of the order they are inputed, or what?

    Can you input numbers and print them out without sorting?

    ps. This should be a fresh thread
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Quote Originally Posted by inkonsinathi View Post
    writing a program that can input numbers and and print them in a rerve order.this problem is giving me troubles pliz me
    You really ought to make a new thread for this... and say what you need help with. There's probably source code for this kind of stuff online anyway.

    I got a PDF (was it The C Programming Language?) that walked through literally everything in the C language. It had a lot of examples and tutorials like what you're trying to do here - try reading it.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    on ericwu

    But I really don't know why it couldn't work when I separate parts....sad
    when you had those as separate files... you have called the create function as

    create(front, end); // This is pass by value

    and in create function create(struct data *front, struct data* end) here front will be having
    some other address, space allocated in "stack" and it will be vanised once you get out of the function.

    Hence the "new" pointer that you assigned with the front (in create) will be lost... since the function is taking parameter as pass by value.. this will not be reflected in the main. and so the front will be always having NULL and going in the "first create"

    i hope you understand the point.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in link list
    By Bargi in forum C Programming
    Replies: 2
    Last Post: 07-16-2008, 01:54 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21