Skip to content

Commit cbcceb6

Browse files
author
Joplin Publisher
committed
Update 2026-06-04T10:04:24.672Z
1 parent 1f27d24 commit cbcceb6

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
layout: post
3+
title: String Literals
4+
abbrlink: ea99e230968447eab9eece94f1f5f837
5+
tags:
6+
- C
7+
- TIL
8+
categories:
9+
- zzz
10+
- Sea
11+
date: 1780565409458
12+
updated: 1780566743390
13+
---
14+
15+
## String Literals
16+
17+
```c
18+
char *text = "Hello";
19+
```
20+
21+
**Know**: The memory address of the first character of the string literal "Hello" is assigned to the pointer variable text.
22+
23+
**However**: This string literal is stored in read-only memory.
24+
25+
**Thus**: Cannot modify the literal. Doing so leads to UB.
26+
27+
**Modern Standard**: You are not supposed to assign a literal to non-const pointer.
28+
29+
```c
30+
const char *text = "Hello"; // this way complier stops accidental modfications of the literal
31+
```
32+
33+
**Why in ROM**: Search engine your way
34+
35+
**Want to Modify?**: Declare it as a character array instead.
36+
37+
```c
38+
char text[] = "Hola"; // allocates mutable memory on stack
39+
text[0] = 'C';
40+
```

0 commit comments

Comments
 (0)