0

I already tried Valgrind with the Valkyrie GUI and Code::Blocks, but I did not really know how to create a project to run it in the debugger, or it just did not work. The printf("here") lines were just for checking were the error was, however, it just sometimes returned "here4". It always returned Segmentation fault: 11, and I cannot see any line where I am adressing memory I do not own.

#include <stdio.h>
#include <string.h>
int c, length=-1, n, m, palindrome, line[250];
int half[126];
int half2[126];
char string[250];
char charac[2];
int main(){
          while(c!=EOF){
                       c=getchar();
                       if(c=='\n' || c==EOF){ /*End of line*/
                                            /*Copies first half of line*/
                                            while(n<=length/2){
                                                              half[n]=line[n];
                                                              ++n;
                                                               }
                                            /*Copies second half of line*/
                                            for(n=0, m=length; n<=length/2; ++n, --m){
                                                                                     half2[n]=line[m]; 
                                                                                      }
                                            /*Tests if line is palindrome*/
                                            for(palindrome=1, n=0; palindrome && n<=length/2; ++n){
                                                                                                   if(half[n]!=half2[n])
                                                                                                             palindrome=0;
                                                                                                    }
                                            if(palindrome)
                                                printf("%s\nis a palindrome.\n", string);
                                            length=-1;
                                            n=0;
                                            m=0;
                                            string[0]=0;
                                            while(n<=125){
                                                         half[n]=0;
                                                         half2[n]=0;
                                                         ++n;
                                                          }
                                            n=0;
                                             }
                       else{
                           /*printf("here4");*/
                           ++length;
                           if(length>500)
                                    break; /*prevent overflow*/
                           line[length]=c;
                           sprintf(charac, "%c", c);
                           strcat(string, charac);
                            }
                        }
          return 0;
           }
  • See anything wrong with this? `for(..., n=-1; ...) { ... if (half[n]!=half2[n]) ... }` – Kevin Dec 10 '18 at 20:02
  • Well, but there is a '++n' in the '...' before accessing the arrays, so it will be '0' at least. –  Dec 10 '18 at 20:07
  • 1
    [Nope](https://stackoverflow.com/a/276519/5522303). The increment step is run *after* the body of the loop. – Kevin Dec 10 '18 at 20:09
  • Really? I thought it was the other way round. Guess I´ll just try it out. –  Dec 10 '18 at 20:11
  • Does not change anything, still getting segfault. –  Dec 10 '18 at 20:12
  • 1
    `strcpy(string, 0)` What do you think that does? Hint: Replace `0` with `NULL` (because they are equivalent) and then think about it. – Kevin Dec 10 '18 at 20:15
  • It seems to work if I replace '0' or 'NULL' with '"\0"'. It just does not recognize palindromes, but that is something for a different question, I guess. –  Dec 10 '18 at 20:18
  • 1
    @FantasyCookie17 If you want to empty a string, you can just do `string[0] = 0;`. No need to copy anything. – Barmar Dec 10 '18 at 20:23
  • 1
    @FantasyCookie17 Look at examples of loops that process arrays, they almost all start with `i = 0;`. If the increment happened first, they'd all skip the first element. – Barmar Dec 10 '18 at 20:25
  • Just edited the code a bit, still returns nothing although palindromes are included in the input text. –  Dec 10 '18 at 20:35
  • 2
    Try separating the code reading the input from the code checking for palindromes (i.e. make a function `bool is_palindrome(const char* string)`). Your code looks too complicated for what it's trying to do – Kevin Dec 10 '18 at 20:38
  • @all I corrected some things, it works now. The corrected code can be seen in my question. –  Dec 10 '18 at 20:46

1 Answers1

0

For some reason, everyone used comments instead of answers, so I am going to accept this answer in 5 minutes if nobody wants to add an own answer so the question gets closed.