Segmentation fault while trying to print contents of char?

You should be allocating with malloc((N_a+1) * sizeof(char)) and you should be printing using the %c format specifier, rather than %s .

Up vote 0 down vote favorite share g+ share fb share tw.

I have the following piece of code: FILE *fpa; fpa = fopen(argv2, "r"); if (fpa == NULL) { printf("Error: could not open seqA file! \n"); exit(0); } unsigned int N_a; fscanf(fpa, "%d\n", &N_a); char *seq_a = malloc((N_a+1) * sizeof(char *)); strcpy(seq_a,""); fscanf(fpa, "%s\n", seq_a); fclose(fpa); for(i=0;i SEG FAULT printf("\n"); I am getting a segmentation fault at the printf statement. Argv2 is a file whose contents is: 5 ABCBB Any idea where I might be making a mistake.

C link asked 5 mins agoHello_PG61.

1 char *seq_a = malloc((N_a+1) * sizeof(char *)); should have sizeof(char) instead of the pointer, although that shouldn't segfault. – Pubby 2 mins ago.

You should be allocating with malloc((N_a+1) * sizeof(char)), and you should be printing using the %c format specifier, rather than %s.

Char *seq_a = malloc((N_a+1) * sizeof(char *)); should be: char *seq_a = malloc((N_a+1) * sizeof(char)); If you want to print each char then use %c: printf("%c", seq_ai).

Since you are printing characters, your printf should use format specifier %c.. not %s. %s expects a pointer, so it's treating a character value as a location in memory.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions