Thread: Linked list problem

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    Linked list problem

    I don't know if I'm missing the boat, but this keeps giving me an error, and I'm not even to the 'hard' part of my homework. Actually, I don't get an error when I compile and run the program, but it generates an error in my computer and pulls up the windows error window.

    The whole part of the homework is to create a linked list, print it, and then add a value to it, and print again. I can't seem to get the linked list part correct...what exactly am I doing wrong?
    Code:
    #include <stdio.h>
    #define AMOUNT 5
    struct linkedlist
    
    {
    
    	int value;
    
    };
    
    struct linkedlist value[5];
    
    int main()
    
    {
    
    	int i;
    	
    	printf("Enter five numbers\n");
    	
    	for(i = 0; i < AMOUNT; i++)
    	
    	scanf("%d", value[i]);
    	
    	printf("you have entered the following numbers:");
    	
    	for(i = 0; i < AMOUNT; i++)
    	
    	printf("%d\n", value[i]);
    	
    }

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    My guess is that since scanf() leaves a carriage return (\r\n on windows, \n on UNIX), the next iteration and invoke to scanf() will pick it up and then, when printing the values received from the user's input, it attempts to cast the carriage return into an integer, which obviously fails and leads to the error.

    Try invoking the getchar() function through each iteration after calling scanf().

    By the way, tell us exactly when the program spits the error. Does it reach to print ANY value at all?

    EDIT: Misread your post. I was assuming the 'value' array was of type integer. In that case, there's your problem. Instead of doing:
    Code:
    scanf("%d", value[i]);
    Do:
    Code:
    scanf("%d", &(value[i].value));
    The error was caused to the fact that you were treating a struct-type as an int-type, which lead to the error. Of course, this could've been a valid process if you were using pointers. If you had a pointer to an integer and casted it to a "struct *linkedlist" data-type, it would've copied the integer's 4 bytes into the struct's 'value' member.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    2
    Ah thank you. I have read here multiple times about improperly treating a struct-type as an int-type, and I went ahead and did the same thing.

    Regarding the error message, when I compile and run the program, it compiles fine, and when I type the first number (any number) and hit enter, it gives me a windows error. I have messed up a lot of things during my one month tenure as a C programmer, but I have never actually gotten an error in windows...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM