I seem to have a problem with a simple program that is supposed to count various character types in a file. It always prints zeroes even though the file is not empty at all. I think it has something to do with pointers, could be wrong. I would also like to know if initializing variables is necessary in this case?
void count_char(FILE *f, unsigned *newl, unsigned *let, unsigned *num, unsigned *spec_char);
unsigned newline = 0, number = 0, letter = 0, special_character = 0;
printf("Insert a file path: ");
f_read = fopen(path, "r");
if(f_read == NULL)
perror("The following error occurred");
count_char(f_read, &newline, &number, &letter, &special_character);
printf("File content:\n\tnewline - %u\n\tletters - %u\n\tnumbers - %u\n\tspecial characters - %u\n", newline, number, letter, special_character);
void count_char(FILE *f, unsigned *newl, unsigned *let, unsigned *num, unsigned *spec_char)
while((c = fgetc(f)) != EOF)
if(c == '\n')
When you do something like this:
*newl++;; what happens is that first, the pointer is incremented (i.e. make it point to the next memory location) and THEN it is dereferenced based on operator precedence.
If you want to dereference it and then increment, you have to use parentheses, like this: