Home > Backend Development > C++ > Assigning strings in C

Assigning strings in C

Mary-Kate Olsen
Release: 2025-01-26 08:03:09
Original
984 people have browsed it

The provided text explains the difference between using a character pointer (char *string) and a character array (char string[]) to store and modify strings in C. Let's rephrase it for clarity and improved flow:

The C code examples demonstrate a crucial distinction between pointers and arrays, often a source of confusion for beginners. Consider these snippets:

Assigning strings in C

Example 1 (Segmentation Fault):

<code class="language-c">#include <stdio.h>
int main(void) {
    char *string = "Wello, world!"; // string is a pointer
    string[0] = 'H';              // Attempting to modify a read-only string
    puts(string);
}</code>
Copy after login

This code results in a segmentation fault. Why? Because string is declared as a pointer to a character. This pointer is initialized to point to a string literal ("Wello, world!"), which is typically stored in read-only memory. Attempting to modify the contents of this read-only memory causes a segmentation fault (a memory access violation).

Example 2 (Successful Modification):

<code class="language-c">#include <stdio.h>
int main(void) {
    char string[] = "Wello, world!"; // string is an array
    string[0] = 'H';              // Modifying a writable copy of the string
    puts(string);
}</code>
Copy after login

This code successfully prints "Hello, world!". Here, string is declared as a character array. The compiler allocates space on the stack to hold a copy of the string literal. This copy is writable, allowing modification of string[0] without causing an error.

The Key Difference:

The fundamental difference lies in how the string is stored. In Example 1, the string literal resides in read-only memory, and the pointer simply points to it. In Example 2, a writable copy of the string literal is created and stored in the array on the stack.

Conclusion:

Pointers and arrays are distinct data types in C. While they may appear similar in some contexts, their behavior can differ significantly, especially when dealing with string literals. Always be mindful of this distinction to avoid unexpected errors and segmentation faults. Using character arrays for modifiable strings is generally safer than using character pointers to string literals.

The above is the detailed content of Assigning strings in C. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template