Thread: managing a list -newbie questions-

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    2

    managing a list -newbie questions-

    Hi everybody,
    I'm trying to doan exercise from an old exam of a course I'm following..but I'm stuck with a segmentation fault (with the printf function) and well... I tried many times to modify the code but, so far, I only managed to get a few very strange errors and I really can't understand what's wrong with the functions. Any suggestion? Where am I wrong?
    thank you!

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct elemento {
    	float valore;
    	struct elemento *next;
    } Elemento;
    
    typedef Elemento* List;
    
    //Adds an element at the beginning
    void Push(List *lista, Elemento e){
    	e.next=*lista;
    	*lista = &e;
    }
    
    //returns the value of the first element
    float Read(List lista){
    	float pi=lista->valore;
    	return pi;
    }
    
    //counts the number of elements
    int Count(List lista){
    if(lista->next==NULL)
    	return 1;
    int i;
    List conta=malloc(sizeof(Elemento));
    conta->next=lista->next;
    for(i=1;(conta->next)!=NULL;conta=conta->next)
    	i++;
    free(conta);
    return i;
    }
    
    main(){
    List a=malloc(sizeof(Elemento));
    a->valore = 4.0;
    a->next = NULL;
    List prova=malloc(sizeof(Elemento));
    prova->valore=2.0;
    prova->next=NULL;
    Push(&a,*prova);
    float pi=Read(a);
    printf("%f\n",pi);
    printf("%d\n",Count(a));
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    First, you should learn to indent code.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct elemento {
        float valore;
        struct elemento *next;
    } Elemento;
    
    typedef Elemento *List;
    
    //Adds an element at the beginning
    void Push(List * lista, Elemento e)
    {
        e.next = *lista;
        *lista = &e;
    }
    
    //returns the value of the first element
    float Read(List lista)
    {
        float pi = lista->valore;
        return pi;
    }
    
    //counts the number of elements
    int Count(List lista)
    {
        if (lista->next == NULL)
            return 1;
        int i;
        List conta = malloc(sizeof(Elemento));
        conta->next = lista->next;
        for (i = 1; (conta->next) != NULL; conta = conta->next)
            i++;
        free(conta);
        return i;
    }
    
    main()
    {
        List a = malloc(sizeof(Elemento));
        a->valore = 4.0;
        a->next = NULL;
        List prova = malloc(sizeof(Elemento));
        prova->valore = 2.0;
        prova->next = NULL;
        Push(&a, *prova);
        float pi = Read(a);
        printf("%f\n", pi);
        printf("%d\n", Count(a));
    }
    The first problem in your code is marked.
    You've just set a pointer to a local variable - a variable which is about to go out of scope, and invalidate your pointer.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    2
    Thank you! I didn't think about the scope of the poor variable e...well, I've managed to get it working by modifing the code like this:
    Code:
    void Push(List *lista, Elemento e){
    	List p =malloc(sizeof(List));
    	p->valore = e.valore;
    	p->next = *lista;
    	*lista = p;
    }
    But I coulnd't think of anything smart leaving "e.next = *lista;" as first line (I mean, I would have to copy that value again because or the variable e)...

    Thank you again, hopefully I'll now be able to finish the exercise...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM