-1

I am trying to write a function which compares first two digits of a number, and since itoa is not available, I am using sprintf as found by googling previously to first convert number to string and then compare each digit. For some strange reason, I am getting seg faults when I run this. I tried same thing but using actual strings like strncmp("100", "101", 2) and it gives no issues. The problem is because of two sprintf statements which somehow messes up str2. I cannot find anywhere why this is happening and what I can do to fix it. Spent 2 hours on this before posting here. Would appreciate some help, if possible

int exists;
int id1 = 100;
int id2 = 101;
char str1[12];
char str2[12];
sprintf(str1, "%d", id1);
sprintf(str2, "%d", id2);
exists = strncmp(str1,str2,2);
printf("Res is %d\n", exists);

Edit: I've tried printf both str1 and str2 and they show 100 and 101 respectively. I also tried iterating thru char arrays but got seg fault again. I actually was able to run it successfully when I created a test file and it all ran so I am totally lost

Edit #2: To add context, this program has multiple files in it and some libraries are included in other files. Everything is properly surrounded by #ifndef. Question is can the order of file compilation produce this issue?

  • 3
    The code you've posted looks fine and runs correctly for me. Are you sure this fails for you? Can you create a [sscce](http://sscce.org/)? – simonc Dec 08 '13 at 00:45
  • What happens when you printf the strings? Can you use a debugger to determine which line causes a segfault? – master_latch Dec 08 '13 at 00:45
  • I believe the input parameters of `strncmp` to be pointers, so you would pass `str1` and `str2` as `&str1` and `&str2` thus passing it's address – edwardmp Dec 08 '13 at 00:49
  • 3
    I am pretty sure the & are unnecessary. str1 already is the adddress. str1[0] is the first character so str1 == &str1[0] – master_latch Dec 08 '13 at 00:52
  • I'm actually not 100% sure so you might be right, but it's worth trying. Here is a similar case which also passes the address with & http://stackoverflow.com/questions/17639578/compare-a-character-on-a-char-array-by-using-strcmp – edwardmp Dec 08 '13 at 00:54
  • I've tried printf both str1 and str2 and they show 100 and 101 respectively. I also tried iterating thru char arrays but got seg fault again. I actually was able to run it successfully when I created a test file and it all ran so I am totally lost – user3078855 Dec 08 '13 at 00:58
  • 1
    What compiler / platform are you using? – Mark Sowul Dec 08 '13 at 01:03
  • cplusplus.com doesn't demonstrate a need for the ampersand: http://www.cplusplus.com/reference/cstdio/sprintf/, http://www.cplusplus.com/reference/cstring/strncmp/ – Mark Sowul Dec 08 '13 at 01:04
  • check the return values of sprintf to see if they were successful – bolov Dec 08 '13 at 01:06
  • @edwardmp: passing `&str1` would attempt to pass a variable of type `char (*)[12]` to a function that expects `char *` (give or take `const`). That won't improve things. – Jonathan Leffler Dec 08 '13 at 01:09
  • As already noted by @simonc, you need to go through the mechanics of creating an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)) that 'works' (in this case, that means 'crashes'). It should contain all the code that is necessary to reproduce the crash and no code that is unnecessary. You should have just a `main()` function — or perhaps a `main()` function that calls the function containing this crashing code. As written, in isolation, there should not be any problems with the code. Make sure it compiles cleanly under stringent compilation options. – Jonathan Leffler Dec 08 '13 at 01:14
  • Re 'Edit #2', you need to work out how to find the code that causes the crash, eliminating everything else. You can start with broad brush strokes. Where are your variables declared in the real code? In a sequence like that, or are they separated by other code? – Jonathan Leffler Dec 08 '13 at 01:19
  • 1
    You problem `exists` elsewhere. Bet if your code _only_ consisted of this and `#include`s. it would work. – chux - Reinstate Monica Dec 08 '13 at 02:14
  • Try to debug and see values of each variable on each step. There is no other solution. – Alexander Perechnev Dec 08 '13 at 05:46

3 Answers3

0

Add

#include <string.h>

at the top. Procedure calls in modern C can misbehave in all sorts of ways if no prototype is in scope.

microtherion
  • 3,938
  • 1
  • 15
  • 18
0

Not 100% sure that I found the correct solution but I believe my issue was linked to malformed makefile. Specifically I previously had this:

CC = /usr/local/bin/gcc
CFLAGS = -Wall -g
... (omitting file list)
#build exe from .o files
$(PROJECT) : $(OBJS)
    $(CC) $(CFLAGS) $(OBJS) -o $(PROJECT)

And after I changed CC to CC = gcc, it started working. I had to strip my main.c before though and proceed slowly as noted above by simonc and Jonathan Leffler.

Community
  • 1
  • 1
-1

."..and since itoa is not available,and since itoa is not available..." What the ... ?! Are you kidding us ? Include stdlib.h :)

#include <stdio.h>
#include <stdlib.h>

void main(void)
{
    int exists, id1 = 100, id2 = 101;
    char str1[12], str2[12];

    sprintf(str1, "%d", id1);
    sprintf(str2, "%d", id2);
    exists = strncmp(str1, str2, 2);

    printf("Res is %d\n", exists);
}

Any way ... when i check and test your posted code, all okay. Try to compile with other software (GCC, TurboC, and etc...).

btw, i didint find any logic in that mind when you comparing two string, which have been converted from integer to char, simply you can check that two integers...

  • itoa is non-standard, and thus unavailable unfortunately. Here's the link http://stackoverflow.com/questions/190229/where-is-the-itoa-function-in-linux – user3078855 Dec 08 '13 at 16:55