C语言 将字符串文字作为定义为指针的函数参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14445705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:06:17  来源:igfitidea点击:

Passing a string literal as a function parameter defined as a pointer

cpointers

提问by Dmitry Minkovsky

I am reading the chapter on arrays and pointers in Kernighan and Richie's The C Programming Language.

我正在阅读 Kernighan 和 Richie 的The C Programming Language 中有关数组和指针的章节。

They give the example:

他们举了个例子:

/*?strlen:??return?length?of?string?s?*/
int?strlen(char?*s)
{
????int?n;

????for?(n?=?0;?*s?!=?'
strlen("hello,?world");??/*?string?constant?*/
strlen(array);???????????/*?char?array[100];?*/
strlen(ptr);?????????????/*?char?*ptr;?*/
';?s++) ????????n++; ????return?n; }

And then say:

然后说:

“Since sis a pointer, incrementing it is perfectly legal; s++has no effect on the character string in the function that called strlen, but merely increments strlen's private copy of the pointer. That means that calls like

“由于s是一个指针,递增它是完全合法的;s++对调用 的函数中的字符串没有影响strlen,只是增加 strlen 的指针私有副本。这意味着调用像

0x10203040 : 0x48 [H]
0x10203041 : 0x65 [e]
0x10203042 : 0x6C [l]
0x10203043 : 0x6C [l]
0x10203044 : 0x6F [o]
0x10203045 : 0x20 [' ']
0x10203046 : 0x57 [W]
0x10203047 : 0x6F [o]
0x10203048 : 0x72 [r]
0x10203049 : 0x6C [l]
0x1020304A : 0x64 [d]
0x1020304B : 0x00 [
int strlen(const char *s)
{
    int n;

    for (n = 0; *s != ′##代码##′; s++)
        n++;
    return n;
}

strlen("Hello World");
]

all work.”

所有的工作。”

I feel like I understand all of this except the first call example: Why, or how, is the string literal "hello, world"treated as a char?*s? How is this a pointer? Does the function assign this string literal as the value of its local variable *sand then use sas the array name/pointer?

除了第一个调用示例之外,我觉得我理解所有这些:为什么或如何将字符串文字"hello, world"视为char?*s? 这是一个怎样的指针?函数是否将此字符串文字分配为其局部变量的值,*s然后s用作数组名称/指针?

回答by Aniket Inge

To understand how a string like "Hello World" is converted to a pointer, it is important to understand that, the string is actually hexadecimal data starting at an address and moving along till it finds a NULL

要了解像“Hello World”这样的字符串如何转换为指针,重要的是要理解,该字符串实际上是从地址开始并一直移动直到找到一个地址的十六进制数据 NULL

So that means, every string constant such as "Hello World" is stored in the memory somewhere

所以这意味着,每个字符串常量,例如“Hello World”都存储在内存中的某处

Possibility would be:

可能性是:

##代码##

So, when this function is called with the above values in the memory, [left side is address followed by ':' and the right side is ascii value of the character]

所以,当这个函数在内存中用上面的值调用时,[左边是地址后跟':',右边是字符的ascii值]

##代码##

at that time, what gets passed to strlenis the value 0x10203040which is the address of the first element of the character array.

那时,传递给的strlen0x10203040字符数组第一个元素的地址值。

Notice, the address is passed by value.. hence, strlenhas its own copy of the address of "Hello World". starting from n = 0, following uptil I find \0in the memory, I increment nand also the address in s(which then gets incremented to 0x10203041) and so on, until it finds \0at the address 0x1020304Band returns the string length.

请注意,地址是按值传递的。因此,strlen有自己的“Hello World”地址副本。从 开始n = 0,直到我\0在内存中找到为止,我递增n以及中的地址s(然后递增到0x10203041)等等,直到它\0在地址处找到0x1020304B并返回字符串长度。

回答by ouah

"hello, world"

"hello, world"

is an array of char(type is char[13]). The value of an array of charin an expression is a pointer to char. The pointer points to the first element of the array (i.e., the value of "hello, world"is &"hello, world"[0]).

是一个char(类型是char[13])的数组。表达式中 的数组的值char是指向 的指针char。指针指向数组的第一个元素(即"hello, world"is的值&"hello, world"[0])。

回答by Veger

Note that:

注意:

  • A pointer is (basically) a value pointing to a memory address.
  • A static string like "hello, word"is stored somewhere in memory
  • 指针(基本上)是指向内存地址的值。
  • 像静态字符串一样"hello, word"存储在内存中的某处

So, a pointer could as easily simply point to a static string as to any other (dynamical) structure that is stored in memory (like an array of characters). There is really no difference with the other provided examples.

因此,指针可以简单地指向静态字符串,也可以指向存储在内存中的任何其他(动态)结构(如字符数组)。与其他提供的示例实际上没有区别。

回答by sr01853

Does the function assign this string literal as the value of its local variable *s and then use s as the array name/pointer?

函数是否将此字符串文字分配为其局部变量 *s 的值,然后使用 s 作为数组名称/指针?

Yes

是的

回答by kong_yy

As it says in the first paragraph of the same page (Page 99, K&R2):

正如在同一页的第一段中所说的那样(第 99 页,K&R2):

"By definition, the value of a variable or expression of type array is the address of element zero of the array."

“根据定义,数组类型的变量或表达式的值是数组元素零的地址。”

The value of "hello, world" would be the address of 'h'.

“hello, world”的值将是“h”的地址。