简体   繁体   English

我如何解释这个声明?

[英]How do I interpret this statement?

I have learned till now that the following statement:到目前为止,我已经了解到以下陈述:

char *ch 

declares a pointer and if we want to connect this pointer to any address we have to write it as :声明一个指针,如果我们想将此指针连接到任何地址,我们必须将其写为:

char *c = &n , However I am not able to understand the meaning of following program, char *c = &n ,但是我无法理解以下程序的含义,

#include <stdio.h>

int main()
{
    char *ch = "Hello World" ;

    printf("%s", ch) ;

    return  0 ;

}

Kindly explain me the logic behind the assign part of line 2 and why the Hello world is being printed.请向我解释第 2 行分配部分背后的逻辑以及打印 Hello world 的原因。

Shouldn't it be *ch in place of ch in the printf?它不应该是 *ch 代替 printf 中的 ch 吗?

According to the C Standard (6.3.2.1 Lvalues, arrays, and function designators)根据 C 标准(6.3.2.1 左值、数组和函数指示符)

3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ''array of type'' is converted to an expression with type ''pointer to type'' that points to the initial element of the array object and is not an lvalue. 3 除非它是 sizeof 运算符或一元 & 运算符的操作数,或者是用于初始化数组的字符串文字,否则类型为“array of type”的表达式将转换为类型为“pointer”的表达式键入 '' 指向数组对象的初始元素并且不是左值。 If the array object has register storage class, the behavior is undefined.如果数组对象具有寄存器存储类,则行为未定义。

Thus in this declaration因此在这个声明中

char *ch = "Hello World";

in the right side there is a character literal that has type of the following array char[12] (character literals include the terminating zero '\\0')在右侧有一个字符文字,其类型为以下数组char[12] (字符文字包括终止零 '\\0')

According to the quote this array is converted to an expression that is a pointer to the first character of the array (string literal).根据引用,这个数组被转换为一个表达式,它是一个指向数组第一个字符(字符串文字)的指针。 So pointer ch is initialized by the address of the first character of the array.所以指针ch由数组第一个字符的地址初始化。 The literal itself stored somewhere in the static memory by the compiler.文字本身由编译器存储在静态内存中的某处。

In fact in this statement you are defining two objects: a character array (string literal) and a pointer that points to the first character of the string literal.事实上,在这个语句中,您定义了两个对象:一个字符数组(字符串文字)和一个指向字符串文字第一个字符的指针。

Though string literals in C are not constant arrays nevertheless you may not change them using such pointers.尽管 C 中的字符串文字不是常量数组,但您不能使用此类指针更改它们。 Any attempt to changes a string literal results in undefined behaviour of the program.任何更改字符串文字的尝试都会导致程序出现未定义的行为。

As for this statement至于这个说法

printf("%s", ch) ;

then as ch points to the first character of a string then this string is outputed according to the format specifier %s然后当ch指向字符串的第一个字符时,则根据格式说明符%s输出该字符串

"Hello World:" string will be stored into text segment of your memory and its address is assigned to *ch . "Hello World:"字符串将存储到您内存的文本段中,并将其地址分配给*ch

"Hello World:" is read-only,you cant modified it in later code. "Hello World:"是只读的,后面的代码不能修改。

"Hello World" is a string literal which is read-only. "Hello World"是一个只读的字符串文字。 You can assign a pointer-to- char to a string literal as string literals gets converted to the type char* (pointer-to- char ).您可以将指向char的指针分配给字符串文字,因为字符串文字被转换为char*类型(指向char指针)。 However, you must not write to string literals as String literals are immutable and writing to it or attempting to modify it will invoke Undefined Behavior.但是,您不能写入字符串文字,因为字符串文字是不可变的,写入或尝试修改它会调用未定义行为。

Note that I'm talking about C here请注意,我在这里谈论的是 C

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM