Reputation: 805
I have the following piece of code:
FILE *fpa;
fpa = fopen(argv[2], "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<N_a;i++)
printf("%s", seq_a[i]); ---> SEG FAULT
printf("\n");
I am getting a segmentation fault at the printf statement.
argv[2] is a file whose contents is:
5
ABCBB
Any idea where i might be making a mistake.
Upvotes: 0
Views: 1465
Reputation: 20726
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.
Upvotes: 0
Reputation: 121387
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_a[i]);
Upvotes: 1