C通过引用传递字符串

时间:2018-09-07 17:26:32

标签: c pointers c-strings double-pointer

#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;
}

任何人都可以简短地解释发生了什么吗? 可能很傻,但是我还是问了...

1 个答案:

答案 0 :(得分:2)

打印整数时,请将整数本身传递给plot 'histo.txt' with boxes 。 当打印字符串时,您传递第一个字符的地址。换句话说,您将指针传递给字符串。

相关问题