Alright so I am getting a weird problem with the following code

int toDB[2];
int fromDB[2];
char * param1;
char * param2;

err = pipe(toDB);
if(err==-1)
{
    printf("Error on pipe creation: %d\n", errno);
    exit(1);
}
sprintf(param1, "%d", toDB[0]);

err = pipe(fromDB);
if(err==-1)
{
    printf("Error on pipe creation: %d\n", errno);
    exit(1);
}
sprintf(param2, "%d", fromDB[1]);

The last line of code sprintf(param2, "%d", fromDB[1]); is the line causing the problem, if it is commented out the code runs, if it is not commented out I get a core dump, can anyone see what i am doing wrong?

NOTE: This is not the full code.

You can not write to either of param1 or param2 without pointing them at valid memory. When you create a raw pointer (i.e. char *) it most likely does not point to some place you should (or can) write to.

Try something like:

char param1[SIZE] = {0}, param2[SIZE] = {0};

/* and then */
snprintf (param1, SIZE, "%d", toDB[0]);

Edited 2 Years Ago by L7Sqr

but the first sprintf works, the one that writes toDB[0] to param1

The first sprintf causes undefined behavior just like the second does. Some times undefined behavior means that you get a segfault and some times it seems to work fine. That doesn't change the fact that it's undefined behavior.

This question has already been answered. Start a new discussion instead.