I am trying to add one character at a time to a char array, called buffer. When I try to add a character to buffer[count], I get a segmentation fault. However if I try to add a character to buffer[0], buffer[1], or any other integer offset it runs fine. Here is my code:
#include <stdio.h>
int main(int argc, char *argv[]){
FILE * fp;
char buffer[100];
fp = fopen(*(argv+1), "r");
if(fp == NULL){
printf("File \"%s\" not found!\n", *(argv+1));
return 0;
}
int curr_char;
unsigned int count = 0;
unsigned int min_len;
while(!feof(fp)){
curr_char = fgetc(fp);
if((curr_char >= 32) && (curr_char <= 126)){
buffer[count] = curr_char;
printf("%c", curr_char);
count++;
if(!((curr_char >= 32) && (curr_char <= 126))){
break;
}
}
}
}
Why is buffer[count] not allowed?