Thread: Can't figure out why program crashes

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    20

    Can't figure out why program crashes

    I can't figure out why the program crashes whenever I put %s in the printf statement of the display method in the following code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct cardata{
        
        char carname;
        char carmodel;
        char caryear;
        
    }CarData;
    
    
    struct node{
        
        struct cardata data;
        struct node *next;
        struct node *prev;
    }*start=NULL;
    
    
    main()
    {
        int ch;
        do 
        {
            printf("\n\n\n1. Insert First\n2. Display\n3. Exit\n");
            printf("\nEnter your choice: ");
            scanf("%d", &ch);
            
            switch(ch)
            {
                
                case 1:
                    insert_first();
                    break;
                case 2:
                    display();
                    break;
                case 3:
                    exit(0);
                default:
                    printf("\n\nInvalid choice. Please try again. \n");
                    
            }
            
        } while(1);
    }
    void insert_first(){
        struct node *ptr;
        char carname;
        char carmodel;
        char caryear;
        
        printf("\n\nEnter the car name: ");
        scanf("%s", &carname);
        printf("\n\nEnter the car model: ");
        scanf("%s", &carmodel);
        printf("\n\nEnter the car year: ");
        scanf("%s", &caryear);
        
        if(start==NULL){
            
            start=(struct node *)malloc(sizeof(struct node));
            start->data.carname=carname;
            start->data.carmodel=carmodel;
            start->data.caryear=caryear;
            start->prev=NULL;
            start->next=NULL;
            
        }else{
            
            ptr=start;
            start=(struct node *)malloc(sizeof(struct node));
            start->data.carname=carname;
            start->data.carmodel=carmodel;
            start->data.caryear=caryear;
            start->prev=NULL;
            start->next=ptr;
            ptr->prev=start;
            ptr->next=NULL;
            
        }
        
    }
    
    
    void display()
    {
        struct node *ptr=start;
        int i=1;
        
        if(ptr == NULL){
            printf("\nLinklist is empty.\n");
        }else{
            printf("\nSr. No\tAddress\tCar Name\tPrevious    Next\n");
            while(ptr != NULL){
                printf("\n%d.\t%d\t%s\t%d\t%d\n", i, ptr, ptr->data.carname,ptr->data.carmodel,ptr->data.caryear,ptr->prev,ptr->next);
                ptr = ptr->next;
                i++;
            }
        }
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Compile with your warnings turned all the way up:
    Code:
    $ make foo
    gcc -Wall -ggdb3 -std=c99 -o foo foo.c -lm -lpthread
    foo.c:22:1: warning: return type defaults to ‘int’ [enabled by default]
    foo.c: In function ‘main’:
    foo.c:35:9: warning: implicit declaration of function ‘insert_first’ [-Wimplicit-function-declaration]
    foo.c:38:9: warning: implicit declaration of function ‘display’ [-Wimplicit-function-declaration]
    foo.c: At top level:
    foo.c:49:6: warning: conflicting types for ‘insert_first’ [enabled by default]
    foo.c:35:9: note: previous implicit declaration of ‘insert_first’ was here
    foo.c:88:6: warning: conflicting types for ‘display’ [enabled by default]
    foo.c:38:9: note: previous implicit declaration of ‘display’ was here
    foo.c: In function ‘display’:
    foo.c:98:7: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘struct node *’ [-Wformat]
    foo.c:98:7: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘int’ [-Wformat]
    foo.c:98:7: warning: too many arguments for format [-Wformat-extra-args]
    All those warnings are potential or actual errors. Some explanations:

    • Line 22: C defaults the return type of any function to int, but that practice is discouraged. Be explicit, int main(void). And return an int at the end, EXIT_SUCCESS or EXIT_FAILURE are appropriate macros to return. They are in stdlib.h, which you already include
    • Lines 35, 38: The compiler only knows about things it's seen so far. You need to put a function prototype or move the definition of your functions above where they are called. Otherwise, the compiler can't check that you are passing the right parameters or using the return value correctly.
    • Lines 49, 88: Since it doesn't have the declaration before use, it assumes the function takes any number or type of parameters and returns int. Again, this prohibits the compiler from doing proper checking. Put prototypes/definitions above first use.
    • Line 98: The extra parameters you pass to printf must match the number and types described by the format string. Getting this wrong, especially with %s, can cause a program crash, since %s expects a pointer to the start of a string. If you give it a bad pointer, your program will attempt to access invalid memory, which results in undefined behavior and may cause a crash.

    Other errors I noticed:

    • Global variables are bad. Read this link. Declare variables in the appropriate functions and pass them around as needed.
    • Your cardata structure has 3 fields, each 1 character long. If you intend to store a string (several characters in a row), you must make those char arrays, so they have room to store all the characters in the string. You have the same issue with the variables you used in insert_first. Your scanf calls are writing to memory they don't own, leading to more undefined behavior. When you make all those fields/variables arrays, you can drop the & from the scanf, since the name of the array is a pointer to the first element. Also, you will need to use strcpy to copy the data into the structure (you can't assign strings with =).

    That is what I got in my 5 minute review. There's likely to be more errors, but this should be more than enough to keep you busy for a while. Fix all this, test your program, and come back if you have more questions.
    Last edited by anduril462; 07-30-2013 at 12:39 PM. Reason: formatting

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    GAHHH!!!!

    Why did you start a new thread for the same topic. One thread, one topic. Be patient! 2 hours is hardly a long time to wait for good, free help.

    This thread is a duplicate of: Help??How to solve this error??. Mods, could you please lock/merge this somehow?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Crashes
    By astroboy739 in forum C Programming
    Replies: 11
    Last Post: 08-20-2012, 06:21 AM
  2. Replies: 17
    Last Post: 12-10-2011, 03:36 PM
  3. My program crashes :|
    By Testify in forum C++ Programming
    Replies: 12
    Last Post: 06-27-2007, 11:48 AM
  4. program crashes
    By Strut in forum Linux Programming
    Replies: 3
    Last Post: 02-10-2002, 10:49 AM
  5. Program crashes and I can't figure out why
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-19-2001, 05:33 PM

Tags for this Thread