Segmentation Fault hmm?

This is a discussion on Segmentation Fault hmm? within the C Programming forums, part of the General Programming Boards category; Hey guys I'm trying to get this to work, it does the first loop in main and asks for the ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Segmentation Fault hmm?

    Hey guys I'm trying to get this to work, it does the first loop in main and asks for the names/year etc, but once I type the year it says "Segmentation fault" and kills the program! If someone could please tell me what Segmentation Fault means that would be great, Also yes I realise I probably should be using fgets instead of fscan but i'm familiar with fscan so I'm sticking with it for this program. But if that's the actual issue then I guess I'll have to change it. Thanks!

    [B]EDIT: Yay I fixed it, someone on IRC told me that I needed & infront of "scanf("%d", records[x].yearborn);" i.e "scanf("%d", &records[x].yearborn);", the only problem is I don't understand WHY :P If someone could help explain that'd be great!

    Code:
    #include <stdio.h>
     
    struct rec
    {
    	char firstname[20];
    	char lastname[20];
    	int yearborn;
    } records[5];
     
    int main()
    {
    	int x;
    	int y;
    	for (x=0; x < 5; x++) 
    	{
    		printf("Please enter the FIRST name of #%d. member!\n", x+1);
    		scanf("%s", records[x].firstname);
    		printf("Please enter the LAST name of #%d. member!\n", x+1);
    		scanf("%s", records[x].lastname);
    		printf("Please enter the year this member was born!\n");
    		scanf("%d", records[x].yearborn);
    	}
    	for (y=0; y < 5; y++)
    	{
    		printf("Record %d, First Name: %s\n", y, records[y].firstname);
    		printf("Record %d, Last Name: %s\n", y, records[y].lastname);
    		printf("Record %d, Year Born: %d\n", y, records[y].yearborn);
    		printf("-------------------------------------\n");
    	}
    	return 0;
    }
    Last edited by pobri19; 05-03-2008 at 06:37 AM.

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Change
    Code:
    scanf("&#37;d", records[x].yearborn);
    to
    Code:
    scanf("%d", &records[x].yearborn);
    EDIT: I see someone already told you the answer 1 minute before i posted =D
    Last edited by 39ster; 05-03-2008 at 06:57 AM.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Yup got that, but knowing WHY would be helpful

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    You were passing the value of yearborn to scanf. In order for a function to modify the value of one of the function parameters, that parameter needs to be the pointer to the variable, not the actual value. Putting "&" in front gets the pointer to the variable so scanf can change the pointers value.

    Heres an example:

    Code:
    void ChangeMe(int a)
    {
      a = 32;
    }
    
    int a = 16;
    ChangeMe(a);
    printf("&#37;i\n", a); //It still prints 16 rather than 32.
    You would use this instead:
    Code:
    void ChangeMe(int* a)
    {
      *a = 32;
    }
    
    int a = 16;
    ChangeMe(&a);
    printf("%i\n", a); //a has been changed after calling ChangeMe
    Last edited by 39ster; 05-03-2008 at 07:01 AM.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    http://cpwiki.sourceforge.net/A_pointer_on_pointers
    It is critical that you understand this.
    Also read http://cpwiki.sourceforge.net/buffer_overrun and fix your scanf lines.
    Even better is to use fgets.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 04:50 PM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

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