Basicaly I want to see if a a number (in my example: 12345678910 ) contains another, smaller number (1234). For this I decided to turn a into a string. For some reason my "in" function never worked. But now for some reason if I ever decide to invoke, there's a segmentation fault BEFORE the program reaches it. Try it out: Compile and run it, then do it again with the line //in(buffer,b); uncommented.

Don't be afraid of the huge lines of code. The problem (I hope) should be quite simple to solve :p

Also, ignore most of the commented "crap", it's just a rough scratch at this point.

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

/*void num_gen (number)
{  
    int hits;
    char string[3];

    itoa(number,string,10);
    
    
	for (i;i<10000;i++)
	{
		if(in(number,i)==1)
			hits++;
	}
return;
}*/

void in (char *a,char *b)
{
	//printf("a: %s \n",a);
	//printf("b: %s \n",b);
	//printf("strlena: %d \n",strlen(a));
	//printf("strlenb: %d \n",strlen(b));
	
	
	char current[strlen(a)+1];
	int i;
	for (i=0;i<=strlen(a)-strlen(b);i++)
	{
		//printf("%c",b[0]);
		int x;
		for (x=0;x<strlen(a);x++)
		{
			//printf("b[i]=%d\n",b[i]);
			current[x]=b[i+x];
			//if (x==strlen(a))
				//current[x+1]="\0";
		}
		//printf ("%s \n",current);
		
	}
return;	
}

int main(void)
{
	int number=1234;

	char buffer[10];
	
	sprintf(buffer, "%i", number);
	printf("Convertion to string is done\n");
	printf("TESTE");
	printf("Number: %s \n",buffer);

	char b[20]="12345678910";
	printf("TEST");
	printf("Num 0: %c",buffer[0]);
	printf("END TESTE");
	
	//in(buffer,b); //the shit hits the fan?
	return 0;
}

>Compile and run it, then do it again with the line //in(buffer,b); uncommented.
It breaks inside if the in function on this line:

current[x]=b[i+x];

And it does this because strlen(a)-strlen(b) in your outer loop condition results in a negative number, yet you increment i from 0.

>Compile and run it, then do it again with the line //in(buffer,b); uncommented.
It breaks inside if the in function on this line:

current[x]=b[i+x];

And it does this because strlen(a)-strlen(b) in your outer loop condition results in a negative number, yet you increment i from 0.

What do you know, it really does. I wonder what happened earlier... :S

Thank you, both :) Does anyone know the right method to terminate a string by the way?

if (x==strlen(a))
	current[x+1]="\0";

Doesn't seem to work as intended.

> Does anyone know the right method to terminate a string by the way?
Right method depends of what you are doing.

current[strlen( a ) + 1] = '\0';
current[ x+1 ] = '\0';

You are trying to use a string "\0" there, instead you have to use '\0', i.e.

if (x==strlen(a))
    current[x+1] = '\0';

and be careful not to access indexes that are out of bounds.

D'oh. Thanks :P C is so different from any other language I've learned so far... I keep missing small details like the F in printf and terminating lines with ";". I'm really glad to have found this forum :) Many thanks, once again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.