Problem with linked lists

This is a discussion on Problem with linked lists within the C Programming forums, part of the General Programming Boards category; I'm having some problems with this simple code: Code: #include <stdio.h> #include <stdlib.h> typedef struct make_point { int x; int ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    Problem with linked lists

    I'm having some problems with this simple code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct make_point {
    	int x;
    	int y;
    	char name[4];
    	}point;
    struct nodea {
    	point naero;
    	struct nodea *prox;
    };
    typedef struct nodea *listap;
    
    void insere_point(listap x, point novo_point)
    {
    	listap t = (listap) malloc(sizeof(listap));
    	t->naero = novo_point;
    	t->prox = x->prox;
    	x->prox = t;
    }
    
    main()
    {
    listap total_point;
    point novo;
    scanf ("%d%d%s",&novo.x,&novo.y,novo.name);
    insere_point (total_point,novo);
    printf ("%d%d%s\n",total_point->naero.x,total_point->naero.y,total_point->naero.name);
    }
    As you can see I just want to do a simple scan and print to and from a list, and when i put this on the standard input "3 15 FRA" i get a random number instead of 3 15 FRA. I've been trying from quite some time to find my mistake but i still can't do this, anyone got a hint to what i'm doing wrong?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Try this, to understand your mistake
    Code:
    listap total_point = NULL;
    point novo;
    scanf ("%d%d%s",&novo.x,&novo.y,novo.name);
    printf("List points to %p\n", (void*)total_point );
    insere_point (total_point,novo);
    printf("List NOW points to %p\n", (void*)total_point );
    printf ("%d%d%s\n",total_point->naero.x,total_point->naero.y,total_point->naero.name);
    > typedef struct nodea *listap;
    Using a typedef just to hide a pointer is pretty poor generally.
    Being able to see instantly how many levels of indirection are present is generally more useful (as it would have been in this case).


    Also, see the FAQ on casting malloc, and why you got the wrong size in your sizeof().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    3

    re salem

    thank you for you reply,
    I tried out the code you wrote and I got the following result:
    "
    List points to 0xbff33f78
    List NOW points to 0xbff33f78
    -107457741610972391
    "
    i had to "listap total_point = NULL;" to listap total_point; because it was giving me a segmentation fault with the first one. Now i can see that the pointer points to the same place after he inserted the new Point, but it's not supposed to? since total_point (the point) should always point to the first node isn't it?
    I also read the faq as you mentioned, on how to cast malloc, i tried to change my malloc(sizeof(listap)); to malloc(sizeof(t)); but i got the same result.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your insere function appears to want to insert the node in the second place always, which is ... a little unusual, but fine. But what are you going to do when there's not anything in the list? You'll need to do something different in that case.

    If you do need to create a new node, you need to create a new node. listap is not a node, and neither is t.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > since total_point (the point) should always point to the first node isn't it?
    Yes, it should be pointing at your first node.
    If you can make it do that, you should be OK.

    > i tried to change my malloc(sizeof(listap)); to malloc(sizeof(t));
    Well it should be sizeof(*t)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists problem
    By needhelpbad in forum C Programming
    Replies: 46
    Last Post: 12-17-2008, 12:12 PM
  2. linked lists inserting problem
    By sara.stanley in forum C Programming
    Replies: 5
    Last Post: 04-02-2006, 12:26 AM
  3. Problem with pointers and linked lists
    By bsimbeck in forum C Programming
    Replies: 2
    Last Post: 02-21-2003, 10:05 AM
  4. linked lists problem
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-17-2002, 10:55 AM
  5. A problem with my linked lists test.
    By valar_king in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:03 PM

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