在C语言中向函数传递指针
直截了当地说,既然C将指针作为参数传递给函数,为什么在交换函数中printf下面的程序没有打印与主函数中pinrtf相同的地址(我认为指针传递正确),这里有什么问题吗在C语言中向函数传递指针,c,pointers,C,Pointers,直截了当地说,既然C将指针作为参数传递给函数,为什么在交换函数中printf下面的程序没有打印与主函数中pinrtf相同的地址(我认为指针传递正确),这里有什么问题吗 #include <stdio.h> void swap(char **str1, char **str2) { char * temp = *str1; *str1 = *str2; *str2 = temp; printf("1---(%#x)
#include <stdio.h>
void swap(char **str1, char **str2)
{
char * temp = *str1;
*str1 = *str2;
*str2 = temp;
printf("1---(%#x) (%#x)---\n", &str1, &str2);
printf("2---(%s) (%s)---\n", *str1, *str2);
}
int main ()
{
char * str1 = "this is 1";
char * str2 = "this is 2";
// swap(&str1, &str2);
printf("(%s) (%s)\n", str1, str2);
printf("(%#x) (%#x)\n", &str1, &str2);
swap(&str1, &str2);
printf("(%s) (%s)\n", str1, str2);
printf("(%#x) (%#x)\n", &str1, &str2);
return 0;
}
#包括
无效交换(字符**str1,字符**str2)
{
char*temp=*str1;
*str1=*str2;
*str2=温度;
printf(“1-(#x)(#x)-n-”、&str1和&str2);
printf(“2-(%s)(%s)-n-”、*str1、*str2);
}
int main()
{
char*str1=“这是1”;
char*str2=“这是2”;
//互换(str1和str2);
printf((%s)(%s)\n),str1,str2;
printf(((#x)(#x)\n“,&str1和&str2);
互换(str1和str2);
printf((%s)(%s)\n),str1,str2;
printf(((#x)(#x)\n“,&str1和&str2);
返回0;
}
在交换函数中,您试图打印本地变量str1和str2的地址,该地址与主函数中变量str1和str2的地址不同。尝试将swap函数中的print语句更改为:
printf("1---(%#x) (%#x)---\n", str1, str2);
在swap函数中,您试图打印局部变量str1和str2的地址,这与主函数中变量str1和str2的地址不同。尝试将swap函数中的print语句更改为:
printf("1---(%#x) (%#x)---\n", str1, str2);
这里是交换函数
swap(&str1, &str2);
您发送交换函数,即str1和str2的地址。在交换功能中
void swap(char **str1, char **str2)
您可以创建一个变量来保存地址(即有自己的地址)
使用打印功能
printf("1---(%#x) (%#x)---\n", &str1, &str2);
在这里,您可以打印存储字符地址的变量的地址。如果您打印存储在该地址中的内容,您可以找到您的字符地址。要打印存储的内容,只需像这样使用reguler打印
printf("0---(%#x) (%#x)---\n", str1, str2);
(this is 1) (this is 2)
(0x61fedc) (0x61fed8)
0---(0x61fedc) (0x61fed8)---
1---(0x61fec0) (0x61fec4)---Here is the address of var that store your char address
2---(this is 2) (this is 1)---
(this is 2) (this is 1)
(0x61fedc) (0x61fed8)
在你运行它之后,你会得到这样的东西
printf("0---(%#x) (%#x)---\n", str1, str2);
(this is 1) (this is 2)
(0x61fedc) (0x61fed8)
0---(0x61fedc) (0x61fed8)---
1---(0x61fec0) (0x61fec4)---Here is the address of var that store your char address
2---(this is 2) (this is 1)---
(this is 2) (this is 1)
(0x61fedc) (0x61fed8)
这里是交换函数
swap(&str1, &str2);
您发送交换函数,即str1和str2的地址。在交换功能中
void swap(char **str1, char **str2)
您可以创建一个变量来保存地址(即有自己的地址)
使用打印功能
printf("1---(%#x) (%#x)---\n", &str1, &str2);
在这里,您可以打印存储字符地址的变量的地址。如果您打印存储在该地址中的内容,您可以找到您的字符地址。要打印存储的内容,只需像这样使用reguler打印
printf("0---(%#x) (%#x)---\n", str1, str2);
(this is 1) (this is 2)
(0x61fedc) (0x61fed8)
0---(0x61fedc) (0x61fed8)---
1---(0x61fec0) (0x61fec4)---Here is the address of var that store your char address
2---(this is 2) (this is 1)---
(this is 2) (this is 1)
(0x61fedc) (0x61fed8)
在你运行它之后,你会得到这样的东西
printf("0---(%#x) (%#x)---\n", str1, str2);
(this is 1) (this is 2)
(0x61fedc) (0x61fed8)
0---(0x61fedc) (0x61fed8)---
1---(0x61fec0) (0x61fec4)---Here is the address of var that store your char address
2---(this is 2) (this is 1)---
(this is 2) (this is 1)
(0x61fedc) (0x61fed8)
谢谢你们的帮助,我是C编程的新手,所以我在这里发布的代码可能有点棘手,我想我找到了我想要的答案,这里有一个链接再次感谢大家 谢谢大家的帮助,我是C编程新手,所以我在这里发布的代码可能有点棘手,我想我找到了我想要的答案,这里有一个链接再次感谢大家 C使用传递值<函数中的code>str1和
str2
是该函数的局部变量,因此在任何其他函数中对变量有不同的地址可能您指的是打印str1
和str2
的第一个printf,而不是&str1
和&str2
为什么使用“%\x”
要打印一品脱而不是标准的“%p”
?这里详细回答了一个类似的问题:您有六个不同的printf
调用,总共打印12个值。你能解释一下你认为哪些值不应该与其他值不同吗<函数中的code>str1和str2
是该函数的局部变量,因此在任何其他函数中对变量有不同的地址可能您指的是打印str1
和str2
的第一个printf,而不是&str1
和&str2
为什么使用“%\x”
要打印一品脱而不是标准的“%p”
?这里详细回答了一个类似的问题:您有六个不同的printf
调用,总共打印12个值。你能解释一下你认为哪些价值观不应该与其他价值观不同吗。