1

This is my code. I want to get 5 strings from the user and espeak reads each of them when user interred it. But I get segmentation fault(core dumped) message.

#include <string.h>
#include <malloc.h>
#include <espeak/speak_lib.h>

int test()
{

espeak_POSITION_TYPE position_type;
espeak_AUDIO_OUTPUT output;
char *path=NULL;
int Buflength = 500, Options=0;
void* user_data;
t_espeak_callback *SynthCallback;
espeak_PARAMETER Parm;



char Voice[] = {"English"};

int i=0;
char text[1000];
unsigned int Size,position=0, end_position=0, flags=espeakCHARS_AUTO, *unique_identifier;


    output = AUDIO_OUTPUT_PLAYBACK;

    espeak_Initialize(output, Buflength, path, Options ); 
    espeak_SetVoiceByName(Voice);
    const char *langNativeString = "en_US";
    espeak_VOICE voice={0};

        voice.languages = langNativeString;
        voice.name = "US";
        voice.variant = 2;
        voice.gender = 1;
       Size = strlen(text)+1;    


for (i=0; i<5; i++)
{

scanf("%s ", &text);

printf("%s", text);

    espeak_Synth( text, Size, position, position_type, end_position, flags,
    unique_identifier, user_data );
    espeak_Synchronize( );
fflush(stdout);

}

return 0;
}






int main(int argc, char* argv[] ) 
{
    test();

    return 0;
}

I tried some modification but none of them worked. I want the program works like this:

User input: hi

espeak says: hi

user input: one

espeak says: one

(for 5 inputs)

But when I try to interring more than 4 characters as input,it gives segmentation fault error!

closed as off-topic by thrig, Jeff Schaller, Jaroslav Kucera, roaima, G-Man Dec 12 '17 at 23:57

  • This question does not appear to be about Unix or Linux within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Have you tried compiling your program with -g and then running it through gdb? BTW, this question is probably more appropriate on SO. – NickD Dec 12 '17 at 16:50
  • I don't know it. may you explain more? – user8663682 Dec 12 '17 at 16:57
  • Do man gcc to find out about its -g option. Do man gdb to find out the rudiments of how to use gdb to debug your program. In particular, when you run your program in gdb and get the segmentation fault, you can use the bt command to get a backtrace: where the fault occurred and the function call chain that got you there. – NickD Dec 12 '17 at 17:16
  • (1) What happens if you take out the espeak parts and just try to read strings from the standard input and write them to the standard output? (2) What actually happens when you run the program as is? (2a) When you enter words shorter than five characters, does it speak them? (2b) When you enter words shorter than five characters, does it write them to the standard output? (2c) When you enter words longer than four characters, what happens before the Segmentation fault? … (Cont’d) – G-Man Dec 13 '17 at 2:14
  • (Cont’d) …  (3) You should learn some coding style, e.g., (3a) Indent properly. (3b) Declare variables only at the beginning of a code block. Avoid declarations - executable code - declarations - executable code sequences unless there’s some really good justification; and in that case, at least separate them with blank lines. (3c) Comment more. (3d) Print the return values from all called functions. Ideally, check them for error and handle them appropriately. (4) Stack Exchange code blocks are limited to about 36 lines; code longer than that has to be scrolled. … (Cont’d) – G-Man Dec 13 '17 at 2:14