Unable to compile and run this C program

I'm trying to compile run the below C code on my Windows & Ubuntu machines with both GCC & VC9. However, I'm facing below issues:

On Windows machine:
VC9 Compiles & run ok, GCC compiles fine, but process terminates when program is run.

On Ubuntu machine:
GCC compiles fine, but when run, I'm shown this prompt:
Segmentation Fault (Core Dump).

Need your assistance here. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <string.h>
#include <stdio.h>

int calc_slope(int input1,int input2)
{
	int sum=0;
	int start=input1;
	int end=input2;
	int curr=start;
	
	//some validation:
	if (input1>input2)
		return -1;


	while(curr<=end)
	{
		if (curr>100)
		{
			char *s="";
			int length;
			int left;
			int right;
			int cent;
			
			sprintf(s,"%d",curr);
			length=strlen(s);
			s++;
			do
			{
				//printf("curr=%d char=%c pointer=%d length=%d \n",curr,*s,s,length);
				left = *(s-1) - '0';
				cent = *s - '0';
				right = *(s+1) - '0';
				//printf("curr=%d l=%d c=%d r=%d\n",curr,left,cent,right);
				if ( (cent>left && cent>right) || (cent<left && cent<right) )
				{
					sum+=1; //we have either a maxima or a minima.
				}
				
				s++;
			} while (*(s+1)!='\0');
		}
		curr++;
	}
	
	return sum;
}

int main()
{
	printf("%d",calc_slope(1,150));
	return 0;
}
Last edited on
1
2
3
4
5
char *s="";
//...
left = *(s-1) - '0';
cent = *s - '0';
right = *(s+1) - '0';

You are accessing unallocated memory here.
These statements

1
2
3
			char *s="";
			// other code
			sprintf(s,"%d",curr);


are invalid . First of all any pointer to a string literal may not be changed. And secondly it occupies usually on 32-bit platforms only 4 bytes. Instead of the pointer you should use a character array with appropriate size.
Last edited on
@MiiNiPaa - I'm doing this to convert the char (in my case *s) to an integer. I took the solution from here: http://stackoverflow.com/questions/868496/how-to-convert-char-to-integer-in-c

Do you know any alternative way ?

@vlad - But how come it works with the VC9 compiler ?
@prahladyeri
@vlad - But how come it works with the VC9 compiler ?


Silently.:) Sometimes invalid code works but its result is undefined.
But how come it works with the VC9 compiler ?

It is undefined behavior. It could works at one moment, then you chanhe some unrelevent part of program, everything crashes and you cannot find why, you never thought on that part and spent all week in debugging...

In the ling you provided they working with allocated character.
You made s points somewhere random in the memory, and accesing area around it.
Okies. Since C++ is not a child's play and I'm quite new to it, can you please show me how do I go about correcting this?

As you must have known by reading this code, all it does is calculate the Slope of each number in a loop. For example, consider the number:

"1324"

Lets ignore the first and last chars i.e. 1 & 4. In case of 3, it is greater than the left ("1") and also the right ("2"). Hence, its a maxima. The string pointer s is supposed to contain the string "1324", and inside the loop, I'm calculating the sum of all maxima and minima.
Use std::string. It is safer. Check bound before accessing. Use std::string::at() member function to stopp accessing out of bounds.
Is std::string a part of C language too, or just C++?

Actually, this is part of an assignment where I have to do two different versions for C & C++. So, in the C version, can I use std::string ?
No, you cannot. It is C++ library.
You have to use something like
1
2
3
4
int string_size = 100;
char s[string_size];
strncpy(s, "1324", 1000);
//Do something with s 


Or you can play with char*, malloc() and direct memory management problems.
Topic archived. No new replies allowed.