Reading a list !

This is a discussion on Reading a list ! within the C Programming forums, part of the General Programming Boards category; i have the following code Code: typedef struct nod { int inf; struct nod *next; } nod; int main(int argc,char ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    Reading a list !

    i have the following code
    Code:
    typedef struct nod
    {
            int inf;
            struct nod *next;
    } nod;
    
    
    int main(int argc,char **argv)
    {
    ...
    read(argv[1],a,b,&sgna,&sgnb);
    ...
    }
    
    void read(char *fis,nod *a,nod *b,int *sgna,int *sqnb)
    {
            FILE *f=fopen(fis,"rt");
            int c;
            nod *t;
            if(f==NULL)
            {
                    exit(-1);
            }
    
            a=(nod*)malloc(sizeof(nod));
            t=a;
            while((c=fgetc(f))!='\n')
            {
                    if(c=='-') *sgna=1;
                    else
                    {
                            t->inf=c-'0';
                            t->next=(nod*)malloc(sizeof(nod));
                            t=t->next;
                    }
            }
    /*
            while(a->next!=NULL)
            {
                    printf("%d ",a->inf);
                    a=a->next;
            }
                    printf("\n");
    */
            fclose(f);
    }
    The problem is that outside the read function the nod structure doesn't preserve its values. Inside the function it can be printed just fine.
    What could the problem be ?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    read(argv[1],a,b,&sgna,&sgnb);
    You'll want
    Code:
    read(argv[1],&a,&b,&sgna,&sgnb);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    a and b are declared like this:
    Code:
    nod *a, *b;

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    It sounds like typical pass-by-value stuff.
    Code:
    #include <stdio.h>
    
    void foo(int a)
    {
       a = 42;
       printf("a = %d\n", a);
    }
    
    int main(void)
    {
       int a = 1;
       printf("a = %d\n", a);
       foo(a);
       printf("a = %d\n", a);
       return 0;
    }
    
    /* my output
    a = 1
    a = 42
    a = 1
    */
    Only in your case, the parameter is a pointer that is modified in the function. Either pass a pointer to the pointer or return the pointer.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    if i:
    Code:
    a=b=(nod*)malloc(sizeof(nod));
    in main before calling the function it works just fine!

    thank you!

    ps: how would the code look like if i do a pointer to a pointer ?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by spank
    ps: how would the code look like if i do a pointer to a pointer ?
    Uglier.

    Something like this, perhaps.
    Code:
    void read(char *fis,nod **a,nod *b,int *sgna,int *sqnb)
    {
            FILE *f=fopen(fis,"rt");
            int c;
            nod *t;
            if(f==NULL)
            {
                    exit(-1);
            }
    
            *a=(nod*)malloc(sizeof(nod));
            t=*a;
            while((c=fgetc(f))!='\n')
            {
                    if(c=='-') *sgna=1;
                    else
                    {
                            t->inf=c-'0';
                            t->next=(nod*)malloc(sizeof(nod));
                            t=t->next;
                    }
            }
            fclose(f);
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    But why are you casting malloc?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    why not ?

    is calloc better ?

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    so what should i use instead of malloc ?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use malloc, just stop casting it. Did you even read the link?

    *magic 8-ball*

    "My sources say no."


    Quzah.
    Last edited by quzah; 03-24-2006 at 06:41 PM.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    really sorry... there was a problem with my english. thank you for the advice!

    Great forum!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11:43 AM
  2. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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