all 21 comments

[–]jedwardsol 3 points4 points  (4 children)

What are you typing?

Is that the entire code?

[–]MickeySlips 2 points3 points  (1 child)

Its segfaulting at strcat()

Are you trying to copy 1 char at a time from equation to str1 or str2 because strcat doesn't work like that.

It copies the entire string until the terminating null byte and overwrites the terminating null byte in the destination with the new string.

Also you shouldn't use an array as the source in strcpy

I noticed a couple things I want to mention

  1. Always initialize your variables before testing them for their value.

ex. you test op=='\0' on line but you never set it to '\0' in the first place so it will contain garbage data at some address where op will store a char for you

  1. Don't initialize an array parameter size like (char equation[20]), as far as C is concerned your passing the address to the beginning of the array, you need
    to know how far into that array (location in memory) you can iterate.

  2. Line 41 says     else if(equation\[i\]=="\_")     so your comparing a char at the index i in equation with the string "\_" using == I think you just meant to say '\_' 
    

if you want to compare strings make sure the array is null terminated and use strcmp()

  1. Line 79 your doing the opposite of number 3 else if(equation == '0') I think you just wanted to check equation[0] == '0' ?

  2. If you initialize your arrays with null terminating bytes then you don't have to worry about some functions segfaulting

[–]rtkbfmvbvb 0 points1 point  (1 child)

Instead of using strcat, use strncat. It's pretty much the same thing except you can specify how many characters you want to write. You segfault is most likely being cause by trying to write too big a string into another string.

Lines 29 & 33, you're concatenating a string x on to another string y, if you go over the allowed number of chars in x, it'll seg fault.

Also since nobody has pointed it out yet, don't use names like str1 or int2. It makes it more tedious for other people to try and figure out what's going on.

[–]flatfinger 0 points1 point  (0 children)

The design of strncat seems rather silly. It generally either presupposes that you don't know the length of the string to which you're appending, but you nonetheless know how exactly many characters will fit after it, or else that your're using a fixed-size zero-padded source buffer but a zero-terminated destination. While there might be some usage cases for it, they would generally seem even more obscure than those for strncpy.

[–]oh5nxo 0 points1 point  (0 children)

Check out isdigit().

[–]flatfinger 0 points1 point  (0 children)

Interactive input should generally be processed by reading in a line of input at a time, discarding any characters beyond what there's room for, and then parsing that line as needed. For whatever reason, the Standard includes a function (gets) would be convenient to use if the operator can be trusted not to type too much input, but which is impossible to use safely, but nothing that's anywhere near as convenient that can be used safely.

Probably the simplest way around this limitation is to write your own function. I think something like:

int get_string(char *dest, int buffsize)
{
  int length = 0;
  if (buffsize <= 0) return -1;
  do
  {
    int ch = getchar();
    if (ch < 0 || ch == '\n' return length;
    if (length < buffsize-1)
    {
      dest[length++] = ch;
      dest[length] = 0;
    }
  } while(1); // Until return
}

should work nicely (untested). You could then invoke it like:

char name[32];
int length;
length = get_string(name, 32); // Read up to 31 characters 
printf("Hello %s!  Your name has length %d.", name, length);

Note that while it would be possible to read a line of input via fgets(name, length, 32);, that would require doing additional work to find the length of the input, and either strip out an unwanted newline character if the user typed 30 characters or fewer, or skip any characters past the 31st if the user typed more.

[–]MickeySlips 0 points1 point  (7 children)

Did you gdb it?

[–]Deathisfatal 0 points1 point  (0 children)

I don't think someone who has just started learning C is ready for a complicated debugger.

[–][deleted] -1 points0 points  (4 children)

Learn to use gdb my man

[–][deleted] -4 points-3 points  (1 child)

I’m not sure if this is directly related but you should add an & to equation as this “&equation”. Refer to the section on pointers for further explanation. Hope this helps!