-2

I am trying to write a function which will read two strings stringArray[MAX]="ABADDFDEFBFCCHCGGEHJJI" and popArr[MAX]="ABCDEFGHIJ" and generate an output like this:

A
B-F-D-A
C-F-D-A
D-A
E-G-C-F-D-A
F-D-A
G-C-F-D-A
H-C-F-D-A
I-J-H-C-F-D-A
J-H-C-F-D-A

However I'm getting a Segmentation fault (core dumped) Error. Why? This is my code:

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>

#define MAX 100

size_t strlstchar(const char *str, const char ch)
{
    char *chptr = strrchr(str, ch);
    return chptr - str;
}

int main(){ 
    // Input strings    
    char stringArray[MAX]="ABADDFDEFBFCCHCGGEHJJI";
    char popArr[MAX]="ABCDEFGHIJ";

    int index=2, lenpop, lentemp;
    char usedString[MAX]="";    
    char tempChar;

    lenpop    = strlen(popArr); 

    printf("%c\n", stringArray[0]); 

    for(int i=1;i<lenpop;i++){
        strcpy(usedString, stringArray);
        printf("%c", popArr[i]);
        tempChar = popArr[i];

        while(tempChar!=stringArray[0]){
            while(index%2==0){
                index = strlstchar(usedString, tempChar);
                lentemp = strlen(usedString);
                usedString[lentemp-index-1]=0;
                }   

            printf("-%c", usedString[index-1]);
            tempChar=usedString[index-1];   
            index=2;        
            }
            printf("\n");       
        }

    return 0;
    }

Thanks in advance!

Blind0ne
  • 1,015
  • 12
  • 28
  • 1
    What is the problem statement? It is hard to guess how does the output depend on the input. – Ivan Smirnov Nov 15 '17 at 10:19
  • 2
    A debugger will tell you where the seg fault occurs. Your problem statement is vague. `stringArray` seems to describe edges of a graph and your output should be paths to A. Please edit this into the question. – M Oehm Nov 15 '17 at 10:20
  • If you replace strcpy with strcat, segfault still occurs? – Michi Nov 15 '17 at 10:26
  • You're going to have to explain how that input is supposed to give that output. – Lundin Nov 15 '17 at 11:59
  • Ignore the algorithm! I changed the question accoring asking only about the error occuring. – Blind0ne Nov 15 '17 at 13:32

1 Answers1

2

The segmentation violation occurs in this line:

    usedString[lentemp - index - 1] = 0;

Here, you're trying to find the index from the end, but your strlstchar returns the index from the beginning, although it starts searching from the end. And you want truncate the string at the found character, of course.

Replace this line with just:

    usedString[index] = 0;

and you get the desired output.

M Oehm
  • 28,726
  • 3
  • 31
  • 42
  • Thanks it worked for the first iteration `B-F-D-A` but afterwards the error appears again. – Blind0ne Nov 15 '17 at 13:35
  • Hmmm. I can't reproduce that. Anyway, you seem to have found a solution. – M Oehm Nov 15 '17 at 14:22
  • I'm compilig with `gcc -Wall -O3 -DNDEBUG -static -std=c99 -pipe -o "%e" "%f" -lm` on ubuntu 16.04 – Blind0ne Nov 15 '17 at 14:35
  • If I take the code from your question, just fi what I've shown above and then compile, it works. I don't see how putting the memory on the heap fixes the problem. (You never free the memory from `malloc` or `strdup`, so you have many memory leaks.) I'm also unsure why you activate the heavy optimisation for a simple program. – M Oehm Nov 15 '17 at 14:46