helpl on linked list example

This is a discussion on helpl on linked list example within the C Programming forums, part of the General Programming Boards category; Hi all! I'm quite new to programming... well, I have to learn it for my exam (which is tommorow by ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    3

    helpl on linked list example

    Hi all!

    I'm quite new to programming... well, I have to learn it for my exam (which is tommorow by the way). I tried to do an exercise on linked list... but I've been sitting behind my desk for a few hours now and still didn't find out what the heck is wrong with my code.
    So here it is:
    Code:
    #include <stdio.h>
    //definiram strukturo node (vozel)
    struct node
    {
    	int amount;
    	struct node *next;
    	char *title;
    };
    
    //prototipi funkcij
    struct node *add(struct node*);
    struct node *display(struct node*);
    
    
    int main(int argc, char *argv[])
    {
    	struct node *head;
    	head->title="test";
    	head->amount=5;
    	add(head);
    	return 0;
    }
    
    //funkcija za izpis seznama zaloge
    struct node *display(struct node *head)
    {
    	printf("\nIZPIS SEZNAMA ZALOGE:\n");
    	struct node *z;
    	z = head;
    	while(z != NULL)
    	{
    		printf("%d....Title: %s \t Amount: %d\n", z->title, z->amount);
    		z = z->next;
    	}
    	printf("\n\n");
    	return head;
    }
    
    //funkcija za dodajanje elementa na konec linked lista
    struct node *add(struct node *head)
    {
    	struct node *new, *first;
    	printf("Title: ");
    	scanf("%s", new->title);
    	//scanf("%s", &new->title);		I tried this as well but it didn't seem to help
    	printf("Amount: ", new->amount);
    	first = head;
    	while(head->next != NULL)
    	{
    		head = head->next;
    	}
    	head->next = new;
    	return first;
    }
    The program compiles but when I try to run it I get segmentation fault right after I type the first title... so there must be some problem with assigning the string value to title member of node structure I guess.

    Any idea on what is wrong? I tried to help myself with a whole bunch of literature but I didn't find any example with assigning array of characters to a member of a structure.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Some one here needs to read up on dynamic memory allocation. I'll give you two guesses, and a hint. The hint is, it's not me. You don't ever allocate space for anything in your program. My sources tell me you're about to fail your exam.


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

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    3
    OK, I fixed the first problem... not allocating any memory at all

    And I also removed string (array of chars) from the structure cuz I had problems with that.
    So, now my code works - I can add new element to the list and display the whole list. But I'd still like to know how to read string from stdin and store it into an array of characters properly.

    The following line of code doesn't work. I also tried to read the string and store it into a temporary array of chars and then assign it to the title member of this structure and that also didn't wok
    Code:
    scanf("%s", &node->title);

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Consider reading the FAQ on how to get input from the user. As a hint, if you plan on using scanf: You only need the & operator for scanf if you aren't using a pointer already.

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

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    3
    Quote Originally Posted by quzah
    You only need the & operator for scanf if you aren't using a pointer already.
    Quzah.

    Oh ........... it was that. Some of you might think this is incredibly stupid mistake... but seriously nobody ever told me this and I didn't read it anywhere else

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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