gpt4 book ai didi

C通过引用传递字符串到函数

转载 作者:太空宇宙 更新时间:2023-11-04 07:53:30 24 4
gpt4 key购买 nike

#include<stdio.h>

void fun1(int **iptr){
printf("%d ", **iptr); //shows value
}

void fun2(char **sptr){
//printf("%s", **sptr); shows segmentation fault
printf("%s", *sptr); //shows string
}

int main(){
char *str = "Hi";
int *x, a = 10;

x = &a;
fun1(&x);
fun2(&str);
return 0;
}

谁能简单解释一下这是怎么回事?可能很傻,但我还是问了...

最佳答案

打印整数时,将整数本身传递给 printf。当打印一串字符时,您传递第一个字符的地址。换句话说,您将指针传递给字符串。

关于C通过引用传递字符串到函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52227138/

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