Problem with a little program.

Hello!
I am new here and I have a question.

I am trying to do a more complex program, but I stumbled across a problem with one of my functions. I'll post a simpler version of the function written as a program here:

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

int main()
{
int p,i=0;
char *q;
char x[3][60], y[3][60];
strcpy(x[0],"Hello! : How are you today?");
strcpy(x[1],"Fine! : How about you?");
for(i=0;i<2;i++)
{ q=strstr(x[i],": ");
p=q-x[i];
strncpy(y[i],x[i],p);
y[i][p]='\0';
printf("%s \n", y[i]);
}
getch();

}


This program works as it should (it needs to display the string found in x[0] and x[1] untill it finds ": "), except it crashes everytime (I use DEV C++ as compiler). Can anyone tell me what the problem is here?

Thank you!
Last edited on
It could be due to this:
fclose(f);
you have not opened any file but trying to close.

Also, check the return value of strstr(), in case it returns null.
The fclose and FILE*f were there from the function I used. But that was the problem here in this program, fclose(f).


The problem in my program was different, but I managed to identify it and fix it after I made this work with your help.

Thank you!
Topic archived. No new replies allowed.