i didnt get a chance to reply sooner because i was disconnected while i was in the middle of typing a reply. needless to say it was big and i don't really feel like retyping it, so here's what i'll do:
filker0 is bang on in what he said - the segmentation error is because of the character not being found. that is why in the link to the example of fgets uses this code:
Code:
if ((p = strchr(buf, '\n')) != NULL)
*p = '\0';
what happens with strchr is that it returns a pointer to the memory location in which the search item is found - if nothing is found it returns NULL. see that in the above snippet, the only time that an operation involving p is performed, it is in the case of the pointer NOT being equal to NULL -search found something!
now i hope that we all have managed to shed some light on your problem, but i get the feeling that its possible that we havent. the code you supplied last still doesnt work, and suggesting further ways to proceed might be a little convoluted, so against my normal policy of trying to help someone realise how to solve the problem, i will instead post code of how i would go about my interpretation of what your goal is (i'm pretty sure that you want to search a string for a certain character). here it is:
Code:
#include<stdio.h>
#include<string.h>
int main()
{
char a[100];
char *p;
int ch;
printf("Enter a string: ");
fgets (a,100,stdin); /*using fgets despite using scanf below,
the reason? i dont have to flush the input,
i do need to take care of the possibility
of a newline, as follows */
if ((p = strchr(a,'\n')) != NULL) /*trust me when i say to always use an if statement when calling strchr*/
*p = '\0'; /*sets newline character (if found) to string terminator character*/
printf("Now enter a single character which you want to search for: ");
scanf ("%c", &ch); /*you could even use fgets here again, limited to one character*/
if ((p = strchr(a,ch)) != NULL) /*now searching for the character*/
printf ("Character %c was found", *p);
else
printf ("Character was not found");
return 0;
}