Hi,

I dont know why I am getting segmentation fault when adding the line 'int n'

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{

   char* pStr; // no memory allocation
int n;

	printf("enter a string\n");
	fgets(pStr,20,stdin);
    printf("\nvalue of string is %s\n",pStr);

while(*pStr!='\0')
	{
 printf("value of string is %c\n",*pStr);
*pStr++;


}

return 0;

}

Your code doesn't seems to work in my compiler Dev C++, with or without int n.
which compiler are you using.?

Your using a character pointer that hasn't been initialized. Line 8 should be

char* pStr = (char*)malloc(200 * sizeof(char));
/*check allocation here*/

Thanks gerard

char *pStr;

means there is not any allocation in memory rather its just a declaration.

Thanks gerard, the program works fine now.

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.