Segmentation Fault (core dumped) I'm not sure what's wrong need some help

This is a discussion on Segmentation Fault (core dumped) I'm not sure what's wrong need some help within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> #include <malloc.h> typedef struct { char *maker; char *model; double oreading; int date_of_manufacture; int date_of_purchase; ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    6

    Segmentation Fault (core dumped) I'm not sure what's wrong need some help

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <malloc.h>
    
    
    typedef struct
    {
    char *maker;
    char *model;
    double oreading;
    int date_of_manufacture;
    int date_of_purchase;
    int gas_tank_capacity;
    } auto_t;
    
    
    void fill_struct (auto_t a);
    void print_details (auto_t a);
    
    
    int main(void)
    {
       auto_t a;
       fill_struct(a);
       print_details(a);
       return 0;
    }
    
    
    void fill_struct(auto_t a)
    {
       printf("enter who makes the product:  ");
       a.maker = (char *)malloc(20*sizeof(char));
       scanf("%s", &a.maker);
       printf("enter the model:  ");
       a.model = (char *)malloc(20*sizeof(char));
       scanf("%s", &a.model);
       printf("enter the odometer reading:  ");
       scanf("%lf",&a.oreading);
       printf("enter the date of manufacture:  ");
       scanf("%d", &a.date_of_manufacture);
       printf("enter the gas_tank_capacity:  ");
       scanf("%d", &a. gas_tank_capacity);
       printf("enter the date of purchase:  ");
       scanf("%d", &a.date_of_purchase);
    
    
    return ;
    }
    
    
    
    
    void print_details(auto_t a)
    {
       printf("The maker of the product is %s",*a.maker);
       printf("The model type is %s",*a.model);
       printf("The odometer reading is %lf",a.oreading);
       printf("The date of manufacture is %d",a.date_of_manufacture);
       printf("The date of Purchase is %d",a.date_of_purchase);
       printf("The Gas tank capacity is %d",a. gas_tank_capacity);
    
    
    return;
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    Line 34 and 37 remove the '&' arrays are already an address to the first element, and line 56 and 57 remove the '*', you want to print the string not the character.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    When scanning a string, use scanf("%s", str) not scanf("%s", &str). Likewise when printing a string, use printf("%s", str) not printf("%s", *str). If you add an extra & you get a char**; if you add an extra * you get a single char. Neither of which is a string. scanf() only requires & on types like int or float because it needs to put values into those variables. A string, as a pointer, can already be "returned" from the function.

    Since you're passing the variable "a" by value to your functions, the changes that fill_struct makes will not be reflected in main() once the function returns (and hence print_details() won't see anything set). You should either pass a by reference e.g.
    Code:
    void fill_struct(auto_a *a) {
        a->maker = ...
    }
    or return the modified struct a. (This may be less efficient.)

    Finally, return is not necessary from void functions, and malloc.h is ancient and should not be used. These days malloc() is in stdlib.h.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    6
    Very helpful thanks a lot

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    Use a compiler that can check printf/scanf calls to make sure the format strings and parameters are consistent.
    Code:
    $ gcc -g -W -Wall -Wextra bar.c 
    bar.c: In function ‘fill_struct’:
    bar.c:34:4: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat]
    bar.c:37:4: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat]
    bar.c: In function ‘print_details’:
    bar.c:56:4: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
    bar.c:57:4: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault (core dumped)?
    By tkd_aj in forum C Programming
    Replies: 17
    Last Post: 04-18-2012, 02:53 AM
  2. Segmentation Fault (core dumped)
    By sameer2904 in forum C Programming
    Replies: 3
    Last Post: 01-09-2012, 07:37 AM
  3. Segmentation Fault (Core Dumped)
    By pureenergy13 in forum C Programming
    Replies: 3
    Last Post: 11-02-2011, 08:50 AM
  4. Replies: 7
    Last Post: 03-17-2011, 11:55 AM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM

Tags for this Thread


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