1
#include <stdlib.h>
#include <stdio.h>


struct a
{
    a * next;
    double v;
};


void add(struct a* list,double d)
{
    if(!list->next) goto exception; //I know that a lot of programmers have a low opinion about "goto"

    list=list->next;

    list->v=d;

    return;

exception:
    printf("Cannot add a new element to the list\n");
}

int main()
{
    struct a l;
    double j;
    int i;

    for(j=1.0; j<10.0; j+=1.0)
    {   
        l.next= (a*)malloc(sizeof(a));
        add(&l,j);
        printf("%lf ",l.v);
    }
    return 0;
}

This program compiles , but there is a mess in the output:

-92559631349317831000000000000000000000000000000000000000000000.000000 -92559631 349317831000000000000000000000000000000000000000000000.000000 -92559631349317831 000000000000000000000000000000000000000000000.000000 -92559631349317831000000000 000000000000000000000000000000000000.000000 -92559631349317831000000000000000000 000000000000000000000000000.000000 -92559631349317831000000000000000000000000000 000000000000000000.000000 -92559631349317831000000000000000000000000000000000000 000000000.000000 -92559631349317831000000000000000000000000000000000000000000000 .000000 -92559631349317831000000000000000000000000000000000000000000000.000000

Whereas desired is:

1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0

Where is the mistake and how to fix it?

0x6B6F77616C74
  • 2,559
  • 7
  • 38
  • 65

1 Answers1

7

The problem is that l.v in main() is never assigned a value as add() assigns the value to l.next. The assignment of list to list->next is not visible to the caller so l in main() is always the same instance of struct a. Meaning the prinf() is printing the same unitialized double.

Other points:

  • initialize l correctly:

    struct a l = { NULL, 0 };
    
  • malloc() memory for the next instance of struct a within add() and intialize all members.
  • access the most recent next in main(), by returning the address of latest struct a from add() for example.
  • Don't cast the return value of malloc() (and use a C compiler).
Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • Lot of problems in the code.. 1) Problem with traversing the nodes and their elements. 2) Memory management you malloc but dont free 3) you dont need Goto you can use if-else ladder Try figuring it out otherwise i will help tomorrow – Nathan Jan 08 '13 at 12:38
  • `The assignment of list to list->next is not visible to the caller` - why? List l is passed to the add() function by reference... – 0x6B6F77616C74 Jan 08 '13 at 13:01
  • @0x6B6F77616C74, `l` in `main()` is not changed by `list = list->next` as everything in C is passed by value, including pointers. If you wanted to change what `l` was pointing to you need to make `l` a pointer and pass in the address of the pointer (so the argument type would be `struct a**`). See http://c-faq.com/ptrs/passptrinit.html – hmjd Jan 08 '13 at 13:03
  • @hmjd I haven't get entirely what you wrote, but I feel that I'm very close http://pastebin.com/dkpD8MAc – 0x6B6F77616C74 Jan 08 '13 at 15:03
  • @0x6B6F77616C74, you need to deference `list` inside `add()`: `*list = (*list)->next;` for example. – hmjd Jan 08 '13 at 15:10
  • Now I have: `Run-Time Check Failure #3 - The variable 'l' is being used without being initialized.` How to initialize it? – 0x6B6F77616C74 Jan 08 '13 at 15:22
  • Where is the List.. its just a structure having one pointer to a structure which keeps getting instantiated with new memory with old one getting lost. – Nathan Jan 09 '13 at 10:57