Simple DList! It should but wont...

This is a discussion on Simple DList! It should but wont... within the C Programming forums, part of the General Programming Boards category; if print out list, always returns : List is empty!, even if i put names inside Code: #include <stdio.h> #include ...

  1. #1
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78

    Simple DList! It should but wont...

    if print out list, always returns : List is empty!, even if i put names inside

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    struct student *dodaj_na_pocetak(struct student *head, char *ime);
    void ispis(struct student *head);
    
    struct student{
    
    	char ime[20];
    	struct student *next;
    };
    
    struct student *head=NULL;
    int main()
    
    {
    	int sranje;
    	char ime[20];
    	struct student *nova;
    	
    	int x;
    	while(x!=0){
    	printf("\n----Izbornik----");
    	printf("\n");
    	printf("\n1.Dodaj");
    	printf("\n2.Ispis");
    	scanf("%d", &x);
    	switch(x)
    	{
    	case 1:
    		printf("Upisi ime: ");
    		scanf("%s", &ime);
    		dodaj_na_pocetak(head,ime);
    		break;
    	case 2:
    		ispis(head);
    		break;
    		}
    	}
    		scanf("%d", &sranje);
    }
    
    struct student *dodaj_na_pocetak(struct student *head, char *ime){
    
    	struct student *prvi;
    	
    	prvi = (struct student *)malloc(sizeof(struct student));
    	strcpy(prvi->ime,ime);
    	prvi->next=head;
    
    	head = prvi;
    	return prvi;
    }
    void ispis(struct student *head){
    
    	struct student *temp;
    	temp = head;
    
    		if(temp==NULL)
    		{
    			printf("Lista je prazna");
    			return;
    		}
    	else
    			while(temp!=NULL)
    		{
    				
    			printf("%s", temp->ime);
    			printf("\n");
    			temp=temp->next;
    		}
    	}
    ...and aprentice shall become master...or not...

    "Never let your sense of moral prevent you from doing what is right!" Salvor Hardin, mayor of Terminus

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are setting "head = prvi", but head is a local variable (argument) in that function, so when you get out of the function, it's still going to be whatever it was before. You need to add another * and do "*head = prvi".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > dodaj_na_pocetak(head,ime);
    The function returns a result - don't you think it would be a good idea to do something with it?

    > #include <malloc.h>
    malloc is declared in stdlib.h, which you include.
    This is just a non-portable duplication of effort.

    Also, read the FAQ on casting the result of malloc in C programs.
    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.

  4. #4
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    Salem ->
    The function returns a result - don't you think it would be a good idea to do something with it?
    Thanks man!! So simple....Well it not the first time, I was looking for error for 1h, and look what it was?? it makes you go crazy!
    thanks again!
    ...and aprentice shall become master...or not...

    "Never let your sense of moral prevent you from doing what is right!" Salvor Hardin, mayor of Terminus

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 10:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

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