-2

When I execute the code below,

#include <stdio.h>
#include <string.h>
int main ( ){
    char string [] = "Home Sweet Home";
    printf ("%s",(char*)memmove(string,&string[5],10));
    }

the output is "Sweet Home Home".

But; when I change the code like below,

#include <stdio.h>
#include <string.h>

int main ( )
{
    char* string = "Home Sweet Home";
    printf ("%s",(char*)memmove(string,&string[5],10));

}

it gives segmentation fault.

What changes when I define this array as a char pointer?

  • `string` in 2nd case points to memory that should not be attempted to be changed. – chux - Reinstate Monica Jan 19 '20 at 12:17
  • But for what you are saying shouldn't I have used const at the beginning? – Nimbostratus Jan 19 '20 at 12:19
  • And some string functions can be used when defined like the 2nd one – Nimbostratus Jan 19 '20 at 12:20
  • 1
    Any attempt to modify a literal string leads to *undefined behavior*. In the second case your pointer `string` points to the first character of such a literal string. That's the reason all such pointers are recommended to be `const`, as in `const char *string`. – Some programmer dude Jan 19 '20 at 12:21
  • Code could have used `const char* string = "Home Sweet Home";` then the compiler should warn/error about `memmove(string,&string[5],10)` – chux - Reinstate Monica Jan 19 '20 at 12:21
  • @Someprogrammerdude if so is there a way to modify these kind of defined strings? – Nimbostratus Jan 19 '20 at 12:22
  • 1
    @burr4ch Except copy them into an array you define yourself, and modify your own array? No, there's no way. – Some programmer dude Jan 19 '20 at 12:25
  • Does this answer your question? [replacing chars with memmove in char pointer](https://stackoverflow.com/questions/18944312/replacing-chars-with-memmove-in-char-pointer) – sshashank124 Jan 19 '20 at 12:28
  • 1
    burr4ch, "the difference between defining a string as an array or as a char pointer?" implies a mis-understanding. A _string_ is not a pointer. Pointers to _strings_ are often used as in the 2nd case - in that case incorrectly. Also trouble with "when I define this array as a char pointer" --> An array is not a pointer, a pointer is not an array. – chux - Reinstate Monica Jan 19 '20 at 12:31

1 Answers1

2

What changes when I define this array as a char pointer?

In this case the most importantly: the mutability of the data changes.

char string [] = "Home Sweet Home";

The "Home Sweet Home" here is an initializer for array string. It initializes the string with the characters with zero terminating character. The array size is inferred from the initializer, (if I count right) that's 16 characters. Array string is declared as char, so it mutable and you can change it.

char* string = "Home Sweet Home";

The "Home Sweet Home" here is a string literal. String literals are immutable, not modifiable, cannot be modified. A pointer to the string literal is stored in string pointer. Modifying a string literal results in undefined behavior. Segmentation fault is the error, when a program accesses a memory location that it is not allowed to access. In this case the program tries to write to a memory location that it is not allowed to modify.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    Detail: "String literals .... cannot be modified." and "Modifying a string literal results in undefined behavior." are in conflict. Attempting to modify is UB. It might work, might not, might crash, ... It is UB. – chux - Reinstate Monica Jan 19 '20 at 12:25
  • 1
    chux is correct, and it is important to get the semantics correct. The C standard says it does not define the behavior of attempting to modify a string literal. Telling somebody it is immutable can cause them to make incorrect deductions when diagnosing a bug in a C implementation where the implementation does not prevent a string literal from being modified. It is incorrect to assert, without qualification, that string literals are immutable or that they are not modifiable or that they cannot be modified. – Eric Postpischil Jan 19 '20 at 13:14