-2

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"]
hellow
  • 12,430
  • 7
  • 56
  • 79
Dominik Viererbe
  • 387
  • 2
  • 12
  • 2
    "What is a debugger" is not an acceptable duplicate, especially when we have canonical ones such as in the case of this utterly common FAQ. A list of canonical dupes is available from the [C tag wiki, FAQ section](https://stackoverflow.com/tags/c/info). To answerers: please do some minimum of research effort by checking that list before answering. We don't need to answer this question yet again, your answers add nothing to the site that has not already been posted many times before. – Lundin Oct 15 '18 at 09:53

2 Answers2

3

You are trying to modify a string literal which is undefined behaviour. On most platforms trying to modify a string literal results in a segmentation fault.

You probably want this:

name1 = name2;

instead of:

*name1 = *name2;

Or more likely you want this:

char name1[] = "Franz";
char name2[] = "Otto";

Now name1 and name2 are arrays that are on the stack you can modify as you want. Before only the pointers name1 and name2 were on the stack, but not the actual strings "Franz" and "Otto".

BTW, there is no pointer arithmetic involved in your program.

You should read the chapter dealing with strings and the chapter dealing with pointers in your C text book.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • With your solution both variables Outputs "Otto" but i only wanted to manipulate the first char so that 'name1' Outputs "Oranz"... and "BTW, there is no pointer arithmetic involved in your program." Yeah in this example is no pointer arithmetic, but based on this i want to do string maipulation with pointer arithmetik like '*(name1+2) = *(name2+1)' – Dominik Viererbe Oct 15 '18 at 10:27
  • @Lin0815 then opt for my the _second_ solution : `char name1[] = "Franz";....` – Jabberwocky Oct 15 '18 at 10:28
1

Instead of this

*name1 = *name2;

Put this:

name1 = name2;

Now you're reassigning th pointer that points to the name, not the actual name itself. You can't reassign the actual name because that's part of your program's binary, and therefore read-only.

Also, to reflect that fact and to prevent such errors, change this

char* name1;
char* name2;

to

const char* name1;
const char* name2;

Both variables are in the stack where i should have access.

Good observation! name1 and name2 are indeed on your stack and you can change them as you please, but not *name1 and *name2.

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • But with `name1 = name2;` i store the pointer address of the second pointer in the first pointer. Now both variables Output "Otto" in printf. I want to manipulate the value of the first pointer like `strncpy(name1, name2, 1);` so that name1 Outputs "Oranz". – Dominik Viererbe Oct 15 '18 at 10:15
  • In that case you need to copy that string to where you can edit it. In this example, the stack will do just fine. Try `char buf[32];`, then do `strcpy(buf, "Franz")` and now you can do `strncpy(buf, name2, 1)` to get "Oranz" when you print `buf`. – Blaze Oct 15 '18 at 10:27