Getting started
Function pointers
Operators
Data types
Arrays
Undefined behavior
Undefined behavior
Dereferencing a pointer to variable beyond its lifetime
Copying overlapping memory
Signed integer overflow
Use of an uninitialized variable
Read value of pointer that was freed
Data race
Modify string literal
Using incorrect format specifier in printf
Modifying any object more than once between two sequence points
Passing a null pointer to printf s conversion
Bit shifting using negative counts or beyond the width of the type
Freeing memory twice
Accessing memory beyond allocated chunk
Returning from a function thats declared with Noreturn or noreturn function specifier
Addition or subtraction of pointer not properly bounded
Modifying a const variable using a pointer
Dereferencing a null pointer
Reading an uninitialized object that is not backed by memory
Using fflush on an input stream
Division by zero
Missing return statement in value returning function
Inconsistent linkage of identifiers
Conversion between pointer types produces incorrectly aligned result
Modifying the string returned by getenv strerror and setlocale functions
Random numbers
Preprocessor and macros
Signal handling
Variable arguments
Files and I/O streams
Assertion
Linked lists
Generic selection
X-macros
Function parameters
Pointers
Structs
Sequence points
Command-line arguments
Aliasing and effective type
Compilation
Identifier scope
Bit-fields
Strings
Comman pitfalls
Error handling
Implicit and explicit conversions
Type qualifiers
Valgrind
Typedef
Selection statements
Declaration vs. definitio
Standard math
Boolean
Literals for numbers, characters and strings
Storage classes
Declarations
Formatted Input/Output
Compound literals
Inline assembly
Threads native
Initialization
Structure padding and packing
Memory management
Implementation-defined behaviour
Atomics
Iteration statements, loops, for, while, do-while
Enumerations
Jump Statements
Create and include header files
Testing frameworks
ctype.h characters classification
Pass 2D-arrays to functions
Side effects
Multi-character character sequences
Constrains
Inlining
Unions
Multi-threading
Common idioms and developer practices
Inter-process communication (IPC)
Comments
Contributors

Modify string literal

suggest change

In this code example, the char pointer p is initialized to the address of a string literal. Attempting to modify the string literal has undefined behavior.

char *p = "hello world";
p[0] = 'H'; // Undefined behavior

However, modifying a mutable array of char directly, or through a pointer is naturally not undefined behavior, even if its initializer is a literal string. The following is fine:

char a[] = "hello, world";
char *p = a;

a[0] = 'H';
p[7] = 'W';

That’s because the string literal is effectively copied to the array each time the array is initialized (once for variables with static duration, each time the array is created for variables with automatic or thread duration — variables with allocated duration aren’t initialized), and it is fine to modify array contents.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents