I wrote a little program to practice pointer arithmetic. If I run it, it terminates with code 139, which is a segmentation fault. I have no idea why the line
*name1 = *name2;
leads to a segmentation fault. Both variables are on the stack, where I should have access.
To my understanding this should set the first letter of name1 to the value of the first letter of name2.
main.c file:
#include <stdio.h>
int main()
{
char* name1;
char* name2;
name1 = "Franz";
name2 = "Otto";
printf("Vorher:\n");
printf("Name1: %s\nName2: %s\n", name1, name2);
*name1 = *name2;
printf("Nachher:\n");
printf("Name1: %s\nName2: %s\n", name1, name2);
return 0;
}
If you want to reproduce it. I've run it with the following Dockerfile
FROM gcc:8
COPY ./src /usr/src/myapp
WORKDIR /usr/src/myapp
RUN gcc -o myapp main.c
CMD ["./myapp"]