Miyuru Gunarathna
Miyuru Gunarathna

Reputation: 13

How to fix Segmentation fault (code dumped) in c. I have no idea where is the fault

I am having trouble with segmentation fault (core dumped) in C. I Have no idea where is the wrong. Firstly I use online compiler and then I used fedora ater that there is an error. So here is my code if someone could help me out that would be awesome! How can i fix this?

#include <string.h>

int main() {
  char *upr, *iName;
  int i=1, i2, j, iQty[10], iCode[10], n=1, iPrice=0, mCounter;
  float tAmount;

  printf("\n\n-----------------------------------------------------\n");
  printf("                  REGIONAL COUNCIL\n");
  printf("-----------------------------------------------------\n");
  printf(" Select five items that you want to buy.\n");
  printf(" Oder will be delivering within two days.\n");
  printf("-----------------------------------------------------\n");
  printf(" Item Code          Item Name\n");
  printf("-----------------------------------------------------\n");
  printf("   1                Rice 5kg\n");
  printf("   2                Dhal 1kg\n");
  printf("   3                Canned fish 230g\n");
  printf("   4                Instant noodles 400g\n");
  printf("   5                Coconut 1\n");
  printf("   6                8 Vegetable pack 3kg\n");
  printf("   7                Biscuit 200g\n");
  printf("   8                10 Egg pack\n");
  printf("   9                Face mask 1\n");
  printf("   10               Hand sanitizer 1\n");
  printf("-----------------------------------------------------\n");
  printf(" One customer can buy only 3 peices from one item.\n");
  printf("-----------------------------------------------------\n\n\n");

  while(i<=5)
  {
    if(i==1)
      strcpy(upr, "st");
    else if(i==2)
      strcpy(upr, "nd");
    else if(i==3)
      strcpy(upr, "rd");
    else
    {
      strcpy(upr, "th");
    }
    printf("Enter %d%s item code and quantity: ", i, upr);
    scanf("%d%d", &iCode[i], &iQty[i]);
    i2=1;
    mCounter=0;
    while(i2<i)
    {
      if(iCode[i2]==iCode[i])
      {
        mCounter++;
      }
      i2++;
    }
    if(mCounter>0)
    {
      printf("You picked up this item earlier. Try another one.\n");
      i--;
    }
    if(!((0<iCode[i]) && (iCode[i]<11)))
    {
      printf("Please enter correct item code.\n");
      i--;
    }
    else if(!((0<iQty[i]) && (iQty[i]<4)))
    {
      printf("You can't buy upto 3 pieces from one item.\n");
      i--;
    }

    i++;
  }
  printf("\n\n-----------------------------------------------------\n");
  printf("\t\t\t\t\t   INVOICE\n");
  printf("-----------------------------------------------------\n");
  printf(" Item name\t\t\t\t\tQty\t\t\tPrice\n");
  printf("-----------------------------------------------------\n");
  while(n<=5)
  {
    if(iCode[n]==1)
    {
      iPrice=480;
      iName="Rice 5kg";
    }
    else if(iCode[n]==2)
    {
      iPrice=65;
      iName="Dhal 1kg";
    }
    else if(iCode[n]==3)
    {
      iPrice=100;
      iName="Canned fish 230g";
    }
    else if(iCode[n]==4)
    {
      iPrice=250;
      iName="Instant noodles 400g";
    }
    else if(iCode[n]==5)
    {
      iPrice=100;
      iName="Coconut 1";
    }
    else if(iCode[n]==6)
    {
      iPrice=1000;
      iName="8 Vegetable pack 3kg";
    }
    else if(iCode[n]==7)
    {
      iPrice=200;
      iName="Biscuit 200g";
    }
    else if(iCode[n]==8)
    {
      iPrice=200;
      iName="10 Egg pack";
    }
    else if(iCode[n]==9)
    {
      iPrice=50;
      iName="Face mask 1";
    }
    else if(iCode[n]==10)
    {
      iPrice=1400;
      iName="Hand sanitizer 1";
    }
    printf(" %s\n", iName);
    printf(" \t\t\t\t\t\t\t %d\t\tRs.%d x 0%d\n", iQty[n], iPrice, iQty[n]);
    tAmount+=iPrice*iQty[n];
    n++;
  }
  printf("-----------------------------------------------------\n");
  printf(" Sub Total: \t\t\t\t\t\tRs.%.2f\n", tAmount);
  if(tAmount>3000)
    printf(" Delivery Fee: \t\t\t\t\t\tFree\n");
  else
  {
    printf(" Delivery Fee: \t\t\t\t\t\tRs.250.00\n");
    tAmount+=250;
  }
  printf(" TOTAL: \t\t\t\t\t\t\tRs.%.2f\n", tAmount);
  printf("-----------------------------------------------------\n");
  printf("\t\t\t\t\t  THANK YOU\n");
  printf("-----------------------------------------------------\n\n\n");
  return 0;
}

I am having trouble with segmentation fault (core dumped) in C. I Have no idea where is the wrong. Firstly I use online compiler and then I used fedora ater that there is an error. So here is my code if someone could help me out that would be awesome! How can i fix this?

Upvotes: 1

Views: 160

Answers (2)

ryyker
ryyker

Reputation: 23208

This:

char *upr, *iName;
....
strcpy(upr, "st");

all by itself results in undefined behavior. At this point in your program, anything can happen, and is most likely the reason for your core-dump.

If you have to use a pointer in this way, it needs to be initialized with memory before trying to write to it:

char *st = calloc(10, 1);

If a pointer is not required, simply create a character array:

char st[10] = {0};  

Upvotes: 0

bruno
bruno

Reputation: 32586

you never initialize upr but you do

 strcpy(upr, "st");

that have an undefined behavior, in your case a crash

you never modify it so you can replace

 if(i==1)
  strcpy(upr, "st");
else if(i==2)
  strcpy(upr, "nd");
else if(i==3)
  strcpy(upr, "rd");
else
{
  strcpy(upr, "th");
}

by

if(i==1)
  upr = "st";
else if(i==2)
  upr = "nd";
else if(i==3)
  upr = "rd";
else
  upr = "th";

After that change the code seems to run well :

bruno@bruno-XPS-8300:/tmp$ a.out


-----------------------------------------------------
                  REGIONAL COUNCIL
-----------------------------------------------------
 Select five items that you want to buy.
 Oder will be delivering within two days.
-----------------------------------------------------
 Item Code          Item Name
-----------------------------------------------------
   1                Rice 5kg
   2                Dhal 1kg
   3                Canned fish 230g
   4                Instant noodles 400g
   5                Coconut 1
   6                8 Vegetable pack 3kg
   7                Biscuit 200g
   8                10 Egg pack
   9                Face mask 1
   10               Hand sanitizer 1
-----------------------------------------------------
 One customer can buy only 3 peices from one item.
-----------------------------------------------------


Enter 1st item code and quantity: 1 2
Enter 2nd item code and quantity: 2 3
Enter 3rd item code and quantity: 6 11
You can't buy upto 3 pieces from one item.
Enter 3rd item code and quantity: 6 2
Enter 4th item code and quantity: 10 1
Enter 5th item code and quantity: 5 1


-----------------------------------------------------
                       INVOICE
-----------------------------------------------------
 Item name                  Qty         Price
-----------------------------------------------------
 Rice 5kg
                             2      Rs.480 x 02
 Dhal 1kg
                             3      Rs.65 x 03
 8 Vegetable pack 3kg
                             2      Rs.1000 x 02
 Hand sanitizer 1
                             1      Rs.1400 x 01
 Coconut 1
                             1      Rs.100 x 01
-----------------------------------------------------
 Sub Total:                         Rs.4655.00
 Delivery Fee:                      Free
 TOTAL:                             Rs.4655.00
-----------------------------------------------------
                      THANK YOU
-----------------------------------------------------


bruno@bruno-XPS-8300:/tmp$ 

Note the variable j is unused :

bruno@bruno-XPS-8300:/tmp$ gcc -Wall cd.c
cd.c: In function ‘main’:
cd.c:6:16: warning: unused variable ‘j’ [-Wunused-variable]
   int i=1, i2, j, iQty[10], iCode[10], n=1, iPrice=0, mCounter;
                ^
bruno@bruno-XPS-8300:/tmp$ 

Upvotes: 2

Related Questions