Hi there,
I am a newbie to c programming, in fact I started yesterday. I am facing a problem which I can't understand.
Given the following code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char* buffer = (char *)malloc(sizeof(char)*6);
strncat(buffer, "ab", 2);
strncat(buffer, "de", 2);
printf(buffer);
free(buffer);
return 1;
}
Output:
# gcc -Wall -ansi test.c -o test
# ./test
abde
However, if I change the two strncat lines to...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char* buffer = (char *)malloc(sizeof(char)*6);
strncat(buffer, "%s", 2);
strncat(buffer, "%s", 2);
printf(buffer);
free(buffer);
return 1;
}
# gcc -Wall -ansi test.c -o test
# ./test
Segmentation fault
Question: Why segmentation fault? What's so special about %s that causes this error?
Thanks in advance.
Regards,
CashCow01