传递一个字符串字面量定义为指针的函数参数 [英] Passing a string literal as a function parameter defined as a pointer

查看:153
本文介绍了传递一个字符串字面量定义为指针的函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读在Kernighan和里奇的 C程序设计语言

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

他们给的例子:

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

    for (n = 0; *s != '\0'; s++)
        n++;
    return n;
}

然后说:

既然取值是一个指针,递增它是完全合法的; 取值++ 对在调用指针的的strlen ,而仅仅是增加strlen的私人拷贝函数的字符串没有影响。这意味着,调用,例如

"Since s is 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

strlen("hello, world");  /* string constant */
strlen(array);           /* char array[100]; */
strlen(ptr);             /* char *ptr; */

所有的工作。

我觉得我明白这一切除了第一个调用示例:为什么,或者怎么样,是字符串你好,世界作为处理的char * S ?这怎么是一个指针?是否分配功能这个字符串作为它的局部变量 * S ,然后用取值作为数组名的值/指针?

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 *s and then use s as the array name/pointer?

推荐答案

要了解像Hello World的字符串是如何转换为指针,是要明白,重要的是,该字符串是起始于一个地址实际上十六进制数据并沿,直到它找到移动 NULL

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的存储在存储器的某处

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

可能性是:

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 [\0]

因此​​,当调用该函数与在存储器中的上述数值,[左侧是地址后跟':'和右侧是字符的ASCII值]

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]

int strlen(const char *s)
{
    int n;

    for (n = 0; *s != ′\0′; s++)
        n++;
    return n;
}

strlen("Hello World");

在那个时候,什么被传递给的strlen 是值 0x10203040 这是第一个元素的地址的字符数组。

at that time, what gets passed to strlen is the value 0x10203040 which is the address of the first element of the character array.

通知,地址是按值传递..因此,的strlen 都有自己的Hello World的地址拷贝。从 N = 0 ,以下uptil我觉得 \\ 0 在记忆中,我增加启动ñ,并在地址取值(然后把它增加到 0x10203041 )等,直到它找到 \\ 0 在地址 0x1020304B 键,返回字符串的长度。

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

这篇关于传递一个字符串字面量定义为指针的函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆