0

Hi I don't know why I get the segmentation fault error. I have to, given the pointer to a file (which contains one number per line, like 12232 in the first line and 240 in the second etc..) I have to find and print for each number the cipher that is repeated more in time in the number, line in 12232 it should printf: "the number is 12232 nad the number is 2"

void stampa(FILE *fp) {

    char m[MAX], copia[MAX], *fp_m = m;
    int rip, dim, i, j, massimo=0, valore;

    if (fp == NULL){
        printf("Puntatore a file vuoto.");
        exit(0);
    }

    while(!(feof(fp))){
        fgets(m, MAX, fp);
        fp_m = m;
        dim = strlen(m)-1;
        for (i=0; i < dim; i++){
            copia[i] = fp_m[i];
        }
        copia[i] = '\0';

        for(i=0; copia[i]!='\0'; i++){
            rip = 0;
            if (copia[i] == '*')
                i++;
            else{
                for(j=i; j < dim; j++){
                    if(fp_m[i] == copia[j]){
                        rip++;
                        copia[j] = '*';
                    }
                }
                if (rip == massimo && fp_m[i] > fp_m[valore])
                    valore = i;
                if(rip > massimo){
                    massimo = rip;
                    valore = i;
                }
            }

        }
        massimo = 0;
        printf("Il numero piu' ripetuto in %s e': %c \n", m, fp_m[valore]);
    }

    return ;
}
awwww
  • 23
  • 5
  • Break out your debugger. On what line does the segfault occur? What are the values of all your variables when it happens? – Nate Eldredge Feb 10 '20 at 15:33
  • I think to have understood the problem but I cannot understand why.. I have solved it by using a pointer to the array 'm' and if i use that pointer in all the function all works good, may I know why? – awwww Feb 10 '20 at 15:42
  • Yes I solved that problem just defining a char pointer to that char array and it worked, may i know why I couldn't modify the array directly? – awwww Feb 10 '20 at 16:22
  • I don't understand your explanation as to what you changed - could you show the new code? Unfortunately, by the nature of C, it's quite possible that the code is still incorrect and just happens to work now by chance. – Nate Eldredge Feb 10 '20 at 16:26
  • done, i have posted the code – awwww Feb 10 '20 at 22:33

1 Answers1

0

Here are a couple of bugs:

  1. while(!(feof(fp))){ is always wrong.

  2. for(i=0; copia[i]!='\0'; i++){
        rip = 0;
        if (copia[i] == '*')
            i++;
    

    When copia[i]=='*' then i will be incremented once in the if clause and again at the end of the loop. This will skip a character following the *. In particular, if the string ends in '*' then you will skip over the trailing null byte and start operating on uninitialized memory. You most likely just wanted to do nothing (or possibly continue;) when copia[i]=='*'.

Adding the fp_m pointer didn't make this bug any better or worse, but changing the layout of the stack may have caused the loop to eventually run into a null byte and terminate instead of crashing. That's actually a bad thing, because it made you think the bug was gone when it wasn't. Making unrelated changes until your program runs is an extremely bad habit when programming in C.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82