Thread: What is segmentation fault?

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    11

    Question What is segmentation fault?

    It is suppose to display the message "This Account ID does not exist" but why do I get "segmentation fault"??
    I was able to compile and run the program so it shouldn't be a problem of the compiler.

    Code:
    #include <stdio.h>#include <string.h>
    struct Account 
    {
        char* Account_ID;
        char* Account_Type;
        char* AccountOwner_FirstName;
        char* AccountOwner_LastName;
        float* Balance;
        int* Phone_No;
        int* AccountOwner_ID;
        char* AccountOwner_Address;
        char* AccountOwner_Email;
    }acc1[100];
    
    
    int main(void)
    {     
        struct Account acc1[100];
        int count=0;
              printf("Please enter the account ID which you wish to delete.\n");
                  char* accID;
              scanf("%s",accID);
              int no;
              for(no=0;no<100;no++)
                  {
                   if(strcmp(accID,acc1[no].Account_ID)==0)
                {
                                    acc1[no].Account_ID = "";
                                    acc1[no].Account_Type = "";
                                    acc1[no].AccountOwner_FirstName = "";
                                    acc1[no].AccountOwner_LastName = "";
                                    acc1[no].Balance = 0;
                                    acc1[no].Phone_No = 0;
                                    acc1[no].AccountOwner_Address = "";
                                    acc1[no].AccountOwner_Email = "";
                       }//end if searching is successful
                else {
                    printf("The Account ID you entered does not exist in the system.\n");
                      }//end of else statement when the searching failed. 
              }//end of the for loop for the searching.
    }

  2. #2
    Registered User
    Join Date
    Apr 2013
    Location
    Dhaka,Bangladesh
    Posts
    6
    you declare acc1[100] twice,may be it cause you trouble.

  3. #3
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Code:
    char* accID;
    scanf("%s",accID);
    Read about pointers in your C reference.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    11
    Quote Originally Posted by Anklon View Post
    you declare acc1[100] twice,may be it cause you trouble.
    I tried correcting it here:
    Code:
    struct Account 
    {
        char* Account_ID;
        char* Account_Type;
        char* AccountOwner_FirstName;
        char* AccountOwner_LastName;
        float* Balance;
        int* Phone_No;
        int* AccountOwner_ID;
        char* AccountOwner_Address;
        char* AccountOwner_Email;
    }acc1;


    but i still get the same error when i try to execute it.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please show your complete current code.

    Also please show where you're allocating memory for all those pointers in your structure.

    Jim

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    11
    Quote Originally Posted by Barney McGrew View Post
    Code:
    char* accID;
    scanf("%s",accID);
    Read about pointers in your C reference.
    but the compiler accepts it that way...
    if i try:

    Code:
    char acc1[10];
    scanf("%s",acc1);
    
    char* acc1;
    scanf("%s",*(acc1));
    i would get an error while compiling.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Just because your compiler accepts the first method, doesn't mean it is correct. It's not, you haven't allocated any memory for that pointer.

    The second snippet is also incorrect, the first scanf() is close to being correct, you just need to limit the number of character it will accept.
    Code:
    char acc1[10];
    scanf("%10s",acc1);
    The second is incorrect for several reasons, first you didn't allocate any memory for the pointer, and two you are trying to pass a pointer to a pointer to scanf() when scanf() only wants a pointer to a variable (you don't need the pointer, but you do need to allocate some memory). Three I'm going to overlook the fact that you are trying to declare the same variable name twice.

    Jim

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    11
    Quote Originally Posted by jimblumberg View Post
    Please show your complete current code.

    Also please show where you're allocating memory for all those pointers in your structure.

    Jim
    these are my codes, in full.
    sorry that they are rather long.
    Code:
    #include <stdio.h>
    #include <string.h>
    struct Account 
    {
    	char* Account_ID;
    	char* Account_Type;
    	char* AccountOwner_FirstName;
    	char* AccountOwner_LastName;
    	float* Balance;
    	int* Phone_No;
    	int* AccountOwner_ID;
    	char* AccountOwner_Address;
    	char* AccountOwner_Email;
    }acc1[100];
    
    
    int main(void)
    {     
    x://looping back if the user had quit from the program.
    	printf("Welcome to Global Bank Banking System.\n");
    z://loop if the password or username entered is invalid;
    	//this is the authentication of verifying a bank staff login
    	printf("Please enter your user name: \n");
    	char UserName[] = "GBstaff";
    	scanf("%s", UserName);
    	printf("Please enter your password: \n");
    	char PassWord[] = "globalBANK";
    	scanf("%s", PassWord);
    		//if...else statement to test if the input is the correct username. 
    		if (strcmp(UserName,"GBstaff") == 0) 
    		{
    		 if (strcmp(PassWord,"globalBANK") == 0){
    		 printf("Welcome!\nWhat do you want to do?\n");
    		}
    		 else
    		{
    		 printf("The user name or password you entered is invalid.\nPlease try again.\n");
    		 goto z; 
    		}//end of if...else statement of password
    		}//end of only if statement of username
    		 else
    		{
    		 printf("The user name or password you entered is invalid.\nPlease try again.\n");
    		 goto z; 
    		}//the end of the else statement of the username.
    y://loops from invalid input or after the completion of a task
    	printf("1 => Add a new bank account user\n2 => Delete an existing account user\n");
    	printf("3 => Search for an account\n4 => Search for the details of an account\n");
    	printf("5 => Update the information of an account\n6 => Account Balance Enquiry\n");
    	printf("7 => Withdraw certain amount of money\n8 => Transfer funds to another account user\n");
    	printf("9 => Bills Payment\n0 => Deposit money into an account\n");
    	//giving the structure an object name & becomes an arraylist.
    	struct Account acc1[100];
    	int count = 0;
    	//the selection of the functions listed above.
    	int selection;
    	scanf("%d",& selection);
        	switch(selection) 
    	{
    	 case 1://add a new account user
    		printf("Please enter a new Account ID:[max 10]\n");
    		char a[10];//Account ID
    		scanf("%s",a);
    		getchar();
    		acc1[count].Account_ID = a;
    		printf("Please enter the Account type:[max 25]\n");
    		char b[25];//Account Type
    		scanf("%s",b);
    		getchar();
    		acc1[count].Account_Type = b;
    		printf("Please enter the Owner's First name:[max 100]\n");
    		char c[100];//Owner Fname
    		scanf("%s",c);
    		getchar();
    		acc1[count].AccountOwner_FirstName = c;
    		printf("Please enter the Owner's Last Name:[max 100]\n");
    		char d[100];//Owner Lname
    		scanf("%s",d);
    		getchar();
    		acc1[count].AccountOwner_LastName = d;
    		printf("Please enter the current balance of this account:[min 1000.00]\n");
    		float e;//balance
    		scanf("%f",&e);
    		acc1[count].Balance = &e; 
    		printf("Please enter the phone number of the Owner:\n");
    		int f;//phone
    		scanf("%d",&f);
    		getchar();
    		acc1[count].Phone_No = &f;
    		printf("Please enter a new Owner ID for this new account:[max 11]\n");
    		int g;//account owner ID
    		scanf("%d",&g);
    		getchar();
    		acc1[count].AccountOwner_ID = &g;
    		printf("Please enter the Home Address of the Owner:[max 1000]\n");
    		char h[1000];//address
    		scanf("%s",h);
    		getchar();
    		acc1[count].AccountOwner_Address = h;
    		printf("Please enter the email address of the Owner:[max 50]\n");
    		char i[50];//email address
    		scanf("%s",i);
    		getchar();
    		acc1[count].AccountOwner_Email = i;
    		printf("The new account has been successfully added into the database.\n");   
    	 break;
    	 case 2://delete existing account user
    	      printf("Please enter the account ID which you wish to delete.\n");
                  char* accID;
    	      scanf("%s",accID);
    	      int no;
    	      for(no=0;no<100;no++)
              	{
              	 if(strcmp(accID,acc1[no].Account_ID)==0)
    			{
                                    acc1[no].Account_ID = "";
                                    acc1[no].Account_Type = "";
                                    acc1[no].AccountOwner_FirstName = "";
                                    acc1[no].AccountOwner_LastName = "";
                                    acc1[no].Balance = 0;
                                    acc1[no].Phone_No = 0;
                                    acc1[no].AccountOwner_Address = "";
                                    acc1[no].AccountOwner_Email = "";
               		}//end if searching is successful
    			else {
    				printf("The Account ID you entered does not exist in the system.\n");
    				printf("Returning to the main menu.\n");
    				sleep(5);
    				goto y;
              		}//end of else statement when the searching failed. 
              }//end of the for loop for the searching.
    	 break;
    	 case 3://search account by account ID
    	printf("Please enter the Account ID:");
    	char* aID;
    	scanf("%s", *(aID));
    	int num;
    	for (num=0;num<100;num++)
    	{
    	 if(strcmp(aID,acc1[num].Account_ID)==0)
    			{
    			 printf("%s",acc1[num].Account_ID);
    			 printf("%s",acc1[num].Account_Type);
    			 printf("%s",acc1[num].AccountOwner_FirstName);
    			 printf("%s",acc1[num].AccountOwner_LastName);
    			 printf("%f",acc1[num].Balance);
    			 printf("%d",acc1[num].Phone_No);
    			 printf("%s",acc1[num].AccountOwner_Address);
    			 printf("%s",acc1[num].AccountOwner_Email);
    			}//end of the if statement
    	 else {
    		printf("This Account ID does not exist in the System.");
    	}//end of the for loop
    	 break;
    	 default: 
    	 printf("Invalid input, please enter again.\n");
    	 goto y;
    	 break;
    	}//end of switch
    	
    	return 0;
    }//end of the main class.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First I don't see where you are allocating any memory for all those pointers in your structure. Why are you using pointers instead of just statically allocating memory for those variables?


    Second stop using goto and start using one of the other proper loop constructs like while(), do/while(), or for().
    The following is a segmentation fault waiting to happen:
    Code:
        printf("Please enter your user name: \n");
        char UserName[] = "GBstaff";
        scanf("%s", UserName);
    Why are you assigning a value to UserName then trying to overwrite that constant value with a user entered value?

    Next you should read up on fgets() and think about replacing many of your scanf() calls with this function.

    Too many other problems to even talk about.

    Jim

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    It would probably be better to use character arrays instead of pointers, the values I chose are just for example, you'll need to use the maximum size you would expect for each field in Account:

    Code:
    struct Account 
    {
        char Account_ID[20];
        char Account_Type[10];
        char AccountOwner_FirstName[32];
        char AccountOwner_LastName[32];
        float Balance;
        char Phone_No[16];
        char AccountOwner_ID[20];
        char AccountOwner_Address[20];
        char AccountOwner_Email[32];
    }acc1[100];

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    35
    I faced the similar fault as well... after googling for this error, I found out that we can trace the fault with:


    Compile with debugging function enable:
    1. gcc -g SourceCode.c -o Program
    2. gdb ./Program
    3. At gdb, run

  12. #12
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    If this code gets executed, it will cause a segementation fault:
    Code:
      printf("Please enter the Account ID:");
      char* aID; 
      scanf("%s", *(aID));
    


    It appears that all the other cases avoid this issue by using character arrays:

    Code:
      printf("Please enter the Account ID:");
      char aID[10]; 
      scanf("%s", aID);
    


    The other issue you have is that you're setting every pointer of every instance of acc1[...] to the same character arrays.



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault
    By killero in forum C Programming
    Replies: 2
    Last Post: 05-30-2012, 08:25 AM
  2. Segmentation Fault
    By DeanWinchester in forum C Programming
    Replies: 4
    Last Post: 05-28-2012, 11:23 AM
  3. Segmentation fault
    By sirsmilealot in forum C Programming
    Replies: 12
    Last Post: 02-10-2010, 01:26 PM
  4. Segmentation fault
    By Ron in forum C Programming
    Replies: 14
    Last Post: 06-08-2006, 01:06 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM