gpt4 book ai didi

c - 计算字符串长度时字符串文字和指针的用法

转载 作者:行者123 更新时间:2023-11-30 15:35:26 25 4
gpt4 key购买 nike

下面的代码与计算字符串长度有关

#include <stdio.h>

int strl (char *s);
main ()
{
int len1 = str ("hah");
printf ("%d", len1);
}


int
str (char *s)
{
int length = 0;
while (*s != '\0')
{
length++;
s++;
}

return (length);
}

当我调用函数str时,我将一个字符串文字分配给指针,而不是将字符串文字的地址分配给指针,但事实证明该地址已存储。指针如何存储“hah”的地址而不指定其地址?

最佳答案

“哈” 属于 type char[] 因此,当您将其传递给函数时,它会像任何其他数组一样衰减为指向其第一个元素的指针。

关于c - 计算字符串长度时字符串文字和指针的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22919848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com