Search code examples
cpointersstring.h

Exit code 3221225477 and a segmentation fault error after messing with pointers


`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 ((file = 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.


Solution

  • You appear to have some fundamental misunderstandings about strings in C. Strings in C are char arrays terminated by a null byte ('\0'). If such a char array is not terminated with a null byte, treating it as a string results in undefined behavior.

    As your variable c is just a single character rather than a char array the only way it can be treated as a string is by having it be 0 itself.

            if (&c == ",")
                continue;
    

    Should be a comparison of the variable itself to ','.

            if (c == ',')
                continue;
    

    And you should not be treating a pointer to c as a string that you can pass to strcpy.

    Further issues:

    • You are using strcat with a, but a has no been initialized. char a[40] = {0};
    • a can only contain 40 characters, but you never check that you're not overrunning this buffer.
    • You are assigning the result of getc to a char variable, but getc returns an int. This may cause issues in comparing this value to EOF.