C通过引用传递字符串到函数
C passing string by reference to function
#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
。
当打印一串字符时,您传递第一个字符的地址。换句话说,您将指针传递给字符串。
#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
。
当打印一串字符时,您传递第一个字符的地址。换句话说,您将指针传递给字符串。