简体   繁体   English

与C指针混淆

[英]Confused with C pointers

 #include<stdio.h>

// Function to swap two values but does not work
void swapDoesNotWork (int *ptrX, int *ptrY);

// Function to swap two values and works fine
void swap (int *ptrX, int *ptrY);

void swap (int *px, int *py) {
 int temp;
 temp = *px;
 *px = *py;
 *py = temp;
}

void swapDoesNotWork (int *px, int *py) {
 printf("\n\n");
 int temp;
 temp = *px;
 px = py; 
 py = &temp;
}

int main() {
 int x = 5;
 int y = 10; 
 swapDoesNotWork(&x, &y);
 printf("++++++++++++++++++\n");
 printf("pre x:%d\n", x); 
 printf("pre y:%d\n", y); 
 printf("\n");
 printf("After calling swapDoesNotWork(&x, &y)...\n");
 printf("post x:%d\n", x); 
 printf("post y:%d\n", y); 
 printf("++++++++++++++++++\n\n");
 x = 5;
 y = 10; 
 printf("= = = = = = = = =\n\n");
 printf("pre x:%d\n", x); 
 printf("pre y:%d\n", y); 
 swap(&x, &y);

 printf("\n");
 printf("After calling swap(&x, &y)...\n");
 printf("post x:%d\n", x); 
 printf("post y:%d\n", y); 
 printf("= = = = = = = = =\n\n");
 return 0;

The output of the above program when compiled and executed is: 上面的程序在编译和执行时的输出为:

infi@linux% ./swap_test.o 


++++++++++++++++++
pre x:5
pre y:10

After calling swapDoesNotWork(&x, &y)...
post x:5
post y:10
++++++++++++++++++

= = = = = = = = =
= = = = = = = = =    
pre x:5
pre y:10

After calling swap(&x, &y)...
post x:10
post y:5

As can be seen, the swapDoesNotWork function does not seem to change the values as is the case with the swap function. 可以看出,在swapDoesNotWork功能似乎并没有改变值与案件swap功能。

I am new to C language, coming from mostly scripting background. 我是C语言的新手,主要来自脚本背景。 Can someone help me why swapDoesNotWork functon is not changing the values? 有人可以帮我为什么swapDoesNotWork函数不更改值吗?

Here are all the modifications performed by swapDoesNotWork : 这是swapDoesNotWork执行的所有修改:

 temp = ...
 px = ... 
 py = ...

All of these are assignments to local variables. 所有这些都是对局部变量的赋值。 Local variables are destroyed when the function returns, so swapDoesNotWork has no lasting effects. 函数返回时,局部变量将被销毁,因此swapDoesNotWork没有持久作用。 It only changes variables that are about to stop existing anyway. 它只会更改将要停止存在的变量。

On the other hand, swap contains these lines: 另一方面, swap包含以下行:

 *px = ...
 *py = ...

These are assignments to locations pointed to by px and py , respectively. 这些是分别由pxpy指向的位置的分配。 Even though px and py are local variables themselves, they can point to variables outside of the current function (in this case the function is called as swap(&x, &y) , so they point to the x and y variables in main ). 即使pxpy本身就是局部变量,它们也可以指向当前函数之外的变量(在这种情况下,该函数称为swap(&x, &y) ,因此它们指向mainxy变量)。

With what you are doing you just cancelled the whole point of using pointers. 您所做的只是取消了使用指针的全部要点。

What you are doing is basically the same as this: 您所做的基本上与此相同:

temp = a;
a = b;
b = temp;

which means that you are acting on a copy of the original variables. 这意味着您正在使用原始变量的副本。 So what you do although it is changing the values of the pointers (if you try a printf inside the function the results should be correct) after the function is finished the results disappear. 因此,尽管完成了更改指针的值的操作(如果您在函数内部尝试使用printf,则结果应该是正确的),但是在函数完成之后,结果将消失。 In order for that function to work you would have to pass as an argument a double pointer. 为了使该功能正常工作,您必须将双指针作为参数传递。 Hope I helped 希望我能帮上忙

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM