Ok I've been fighting with this one all morning, finally got it working (I keep coding things as some of you may have noticed the hard way until I step back and go "OH" and put the easy way.) But I wanted the pointer to point to the starting of the second string in the first. However I can only get it to print the character it points to after finding the second string in the first. Also I can only get it working if I leave out the "if null" check for if it did not find the second string in the first.
I suppose the check for null is more important than the pointer working properly, so why is it crashing when I leave in the "if null"?
the idea is that if the string_in function gets to the end of the original string without finding a match it passes the address of the null character in the original string and this is what PTR is supposed to be set to.
Code:
#include <stdio.h>
#include <string.h>
#define MAX 80
int string_in(char * orig, char * check);
int main(void)
{
char ins[MAX+1], fit[MAX+1];
char * ptr;
int done = 0;
printf("Input two strings [q to quit]\n");
while(!done)
{
gets(ins);
if (strcmp("q\0", ins) == 0)
done = 1;
else if (strlen(ins) != 0)
{
gets(fit);
ptr = ins[string_in(ins, fit)];
if (strcmp("\0", ptr) == 0)
printf("\n%s not found in %s\n", ins, fit);
else
printf("\n%s found in %s ||\t %c %p\n - - - - - -\n", fit, ins, ptr, ptr);
printf("Input two strings [q to quit]\n");
}
}
return 0;
}
int string_in(char * orig, char * check)
{
int i = 0, x;
x = strlen(check);
while(orig[i] != '\0')
{
orig += i;
if (strncmp(orig, check, x) == 0)
break;
i++;
}
return (i);
}