在C中尝试遍历字符串数组的方法时,我开发了以下小程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char* string;
int main() {
char *family1[4] = {"father", "mother", "son", NULL};
string family2[4] = {"father", "mother", "son", NULL};
/* Loop #1: Using a simple pointer to step through "family1". */
for (char **p = family1; *p != NULL; p++) {
printf("%sn", *p);
}
putchar('n');
/* Loop #2: Using the typedef for clarity and stepping through
* family2. */
for (string *s = family2; *s != NULL; s++) {
printf("%sn", *s);
}
putchar('n');
/* Loop #3: Again, we use the pointer, but with a unique increment
* step in our for loop. This fails to work. Why? */
for (string s = family2[0]; s != NULL; s = *(&s + 1)) {
printf("%sn", s);
}
}
我的具体问题涉及3号环路的故障。当通过调试器运行时,循环#1和#2成功完成,但最后一个循环由于未知原因失败。我不会在这里问这个问题,除非这表明我对"&"运算符有一些严重的误解。
我的问题(以及目前的理解)是:family2
是一个指向char的指针数组。因此,当s
被设置为family2[0]
时,我们有一个指向"父亲"的(char*)
。因此,取&s
应该给我们family2
的等价物,指向预期指针衰减后的family2
的第一个元素。为什么不呢,*(&s + 1)
是否如预期的那样指向下一个元素?
非常感谢,
生命危机
编辑——更新和经验教训:
以下列表总结了所有相关事实和解释,解释了为什么第三个循环不像前两个循环那样工作。
s
是一个单独的变量,它保存变量family2[0]
的值(指向char的指针)的副本。也就是说,这两个等价的值位于内存中的单独位置family2[0]
到family2[3]
是存储器的连续元素,而s
在这个空间中不存在,尽管它确实包含与循环开始时存储在family2[0]
中的值相同的值- 前两个事实意味着
&s
和&family2[0]
不相等。因此,向&s
添加一个将返回指向未知/未定义数据的指针,而向&family2[0]
添加一个则会根据需要返回&family2[1]
- 此外,第三个for循环中的更新步骤实际上并没有在每次迭代中导致s在内存中前进。这是因为
&s
在循环的所有迭代中都是恒定的。这就是观察到的无限循环的原因
感谢大家的帮助
生命危机
s = *(&s + 1)
时,变量s
是仅包含循环的隐式作用域中的局部变量。当您执行&s
时,您将获得该局部变量的地址,该地址与任何数组都无关。
与上一个循环的不同之处在于s
是指向数组中第一个元素的指针。
为了更"图形化"地解释它,上一个循环中的内容类似于
+----++---++------------+|&s|--->|s|--->|family2[0]|+----++----++---------------+
也就是说,&s
指向s
,而s
指向family2[0]
。
当你做&s + 1
时,你实际上有了类似的东西
+------------+|家族2[0]|+------------+^|+---+----|s|。。。+---+----^^||&s&s+1图片帮助很大:
+----------+
| "father" |
+----------+ +----------+ +-------+ NULL
/-----------→1000 | "mother" | | "son" | ↑
+-----+ ↑ +----------+ +-------+ |
| s | ? | 2000 2500 |
+-----+ | ↑ ↑ |
6000 6008 +----------------+----------------+--------------+--------------+
| family2[0] | family2[1] | family2[2] | family2[3] |
+----------------+----------------+--------------+--------------+
5000 5008 5016 5024
( &s refers to 6000 )
( &s+1 refers to 6008 but )
( *(&s+1) invokes UB )
为简便起见,选择随机整数地址
这里的问题是,尽管s
和family2[0]
都指向字符串文字"father"
的相同基地址,但指针彼此不相关,并且有自己不同的存储位置。*(&s+1) != family2[1]
。
当你执行*(&s + 1)
时,你会点击UB,因为&s + 1
是一个你不应该篡改的内存位置,也就是说,它不属于你创建的任何对象。你永远不知道那里存储了什么=>未定义的行为。
感谢@2501指出了几个错误
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char* string;
int main() {
char *family1[4] = { "father", "mother", "son", NULL };
string family2[4] = { "father", "mother", "son", NULL };
/* Loop #1: Using a simple pointer to step through "family1". */
for (char **p = family1; *p != NULL; p++) {
printf("%sn", *p);
}
putchar('n');
/* Loop #2: Using the typedef for clarity and stepping through
* family2. */
for (string *s = family2; *s != NULL; s++) {
printf("%sn", *s);
}
putchar('n');
/* Loop #3: Again, we use the pointer, but with a unique increment
* step in our for loop. This fails to work. Why? */
/*for (string s = family2[0]; s != NULL; s = *(&s + 1)) {
printf("%sn", s);
}
*/
for (int j = 0; j < 3; j++)
{
printf("%d ",family2[j]);
printf("%dn", strlen(family2[j]));
}
printf("n");
int i = 0;
for (string s = family2[i]; i != 3; s = (s + strlen(family2[i]) + 2),i++) {
printf("%d ",s);
printf("%sn", s);
}
system("pause");
}
这是一个从你的代码中修改的例子,如果你运行它,你会发现点和族的地址发生了变化2,那么你就会理解循环#3的关系。