`The segmentation fault error occurs in the 16th line in the while loop in it’s second iteration.
It seems something I’m not aware of happens after 19th line.
The file in question starts with 255,255,255,255,255… and after a long time it switches back and forth between the values 255 and 0. and instead of printing them I need to put them inside a function, but I can do that myself
(I’m not familiar in any terminology and I have no Idea how to fix it cuz I’m a beginner in c)`
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
char a[40];
char destination[40];
char c;
char * ptr;
FILE *file;
if ((tetkabetka = fopen("file_rgb.txt", "r")) == NULL){
printf("unable to open file");
return 1;
}
while ((c = getc(file)) != EOF){
if (&c == ",")
continue;
char * source = &c;
strcpy(destination, source);
ptr = strcat(a ,destination);
printf("%s", a); //after this I need to empty the string for the next rgb value which has not been yet accomplished.
}
if (fclose(tetkabetka) == EOF){
printf("unable to close file");
return 1;
}
return 0;
}
The compiler shows this weird exit code but it doesn’t print out anything.
The compiler produces these errors when I remove the ‘&’ symbol before the ‘c’ variable.
test.c: In function 'main':
test.c:17:15: warning: comparison between pointer and integer
17 | if (c == ",")
| ^~
test.c:19:25: warning: initialization of 'char *' from 'char' makes pointer from integer without a cast [-Wint-conversion]
19 | char * source = c;
| ^
[Done] exited with code=3221225477 in 0.254 seconds
I don’t understand why it turns c into a pointer (with my limited expertise as I’m a noob)
an explanation would be extremely appreciated.
Kavicky420 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.