I'm having trouble understanding pointers concept and below is the code. Why the swap(&a1, &a2) out puts the -5, 6 rather than 6, -5 ? The values are already swap right?
我无法理解指针概念,下面是代码。为什么交换(&a1,&a2)输出-5,6而不是6,-5?这些值已经互换了吗?
void swap(int *ptr1, int *ptr2){
int temp;
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}
int main (int argc, char *argv[]){
void swap(int *ptr1, int *ptr2);
int a1 = -5;
int a2 = 6;
int *p1 = &a1;
int *p2 = &a2;
NSLog(@" a1 = %i, a2 =%i", a1, a2); // out puts: -5, 6
swap(p1,p2);
NSLog(@" a1 = %i, a2 =%i", a1, a2); // out puts: 6, -5
swap(&a1, &a2);
NSLog(@" a1 = %i, a2 =%i", a1, a2); // out puts: -5, 6
}
3 个解决方案
#1
7
Your first call to swap() swapped the two values, and the second call swapped them back.
你第一次调用swap()交换了两个值,第二个调用交换了它们。
#2
0
You are swapping the same thing every time(sorta) so first you swap them, and then you swap them back. a1 stored the value -5 at address1 (an address is a location in memory) a2 stores the value 6 at addess2 p1 points to addres1 p2 points to address2 swapping p1 and p2 Here you take the value of the item stored at address1 and switch it with the value of the item at address2. so now address1 holds the value 6 and address2 holds the value -5 Here p1 and p2 still point at the same addresses and a1 and a2 still return values from the same addresses but the values have been swapped. swapping &a1 and &a2 Swapping &a1 and &a2 does the same thing, the & returns the addresses as pointers, which you them pass to the swap function. Swap takes to pointers and swaps the values of the addresses they point to.
你每次交换相同的东西(sorta)所以首先你交换它们,然后你交换它们。 a1在地址1处存储值-5(地址是存储器中的位置)a2将值6存储在addess2 p1点到addres1 p2指向地址2交换p1和p2这里你取出存储在地址1的项目的值并切换它使用地址2处的项目值。所以现在address1保持值6,address2保持值-5这里p1和p2仍然指向相同的地址,a1和a2仍然返回相同地址的值,但值已被交换。交换&a1和&a2交换&a1和&a2做同样的事情,&返回地址作为指针,你将它们传递给交换函数。交换接收指针并交换它们指向的地址的值。
Basically, pointers point to a memory location. Your swap function takes to memory locations and exchanges the data stored in them, it does not change what memory address they are pointing to. So in both cases you are sending the same addresses, so you swap the same to location twice.
基本上,指针指向内存位置。您的交换函数占用内存位置并交换存储在其中的数据,它不会更改它们指向的内存地址。因此,在这两种情况下,您都发送相同的地址,因此您将相同的地址交换两次。
#3
0
A very simple example just to get a feeling about pointers...
一个非常简单的例子,只是为了获得关于指针的感觉......
int i = 2; // i == 2
int *p = &i; // p == 0x00AB (say memory addres of i is 171)
int *q = p; // q == 0x00AB q and p have the same value
p == q is true
p == q是真的
*p == 2 is true
* p == 2是真的
*p == *q is true
* p == * q为真
p = NULL; // initializes the pointer, which is a good practice
if (*p == 2) { // don't do this as it can cause error or unpredictable results
i == 2 is still true irrespective of what you did with your pointer variable
无论你对指针变量做了什么,i == 2仍然是真的
Pointer variables could be seen or thought of as holding "special" integer values, they store a memory address, which usually is 32-bit number (unless you are running on a 64-bit address computer).
指针变量可以看作或被认为是持有“特殊”整数值,它们存储一个内存地址,通常是32位数(除非你在64位地址计算机上运行)。
#1
7
Your first call to swap() swapped the two values, and the second call swapped them back.
你第一次调用swap()交换了两个值,第二个调用交换了它们。
#2
0
You are swapping the same thing every time(sorta) so first you swap them, and then you swap them back. a1 stored the value -5 at address1 (an address is a location in memory) a2 stores the value 6 at addess2 p1 points to addres1 p2 points to address2 swapping p1 and p2 Here you take the value of the item stored at address1 and switch it with the value of the item at address2. so now address1 holds the value 6 and address2 holds the value -5 Here p1 and p2 still point at the same addresses and a1 and a2 still return values from the same addresses but the values have been swapped. swapping &a1 and &a2 Swapping &a1 and &a2 does the same thing, the & returns the addresses as pointers, which you them pass to the swap function. Swap takes to pointers and swaps the values of the addresses they point to.
你每次交换相同的东西(sorta)所以首先你交换它们,然后你交换它们。 a1在地址1处存储值-5(地址是存储器中的位置)a2将值6存储在addess2 p1点到addres1 p2指向地址2交换p1和p2这里你取出存储在地址1的项目的值并切换它使用地址2处的项目值。所以现在address1保持值6,address2保持值-5这里p1和p2仍然指向相同的地址,a1和a2仍然返回相同地址的值,但值已被交换。交换&a1和&a2交换&a1和&a2做同样的事情,&返回地址作为指针,你将它们传递给交换函数。交换接收指针并交换它们指向的地址的值。
Basically, pointers point to a memory location. Your swap function takes to memory locations and exchanges the data stored in them, it does not change what memory address they are pointing to. So in both cases you are sending the same addresses, so you swap the same to location twice.
基本上,指针指向内存位置。您的交换函数占用内存位置并交换存储在其中的数据,它不会更改它们指向的内存地址。因此,在这两种情况下,您都发送相同的地址,因此您将相同的地址交换两次。
#3
0
A very simple example just to get a feeling about pointers...
一个非常简单的例子,只是为了获得关于指针的感觉......
int i = 2; // i == 2
int *p = &i; // p == 0x00AB (say memory addres of i is 171)
int *q = p; // q == 0x00AB q and p have the same value
p == q is true
p == q是真的
*p == 2 is true
* p == 2是真的
*p == *q is true
* p == * q为真
p = NULL; // initializes the pointer, which is a good practice
if (*p == 2) { // don't do this as it can cause error or unpredictable results
i == 2 is still true irrespective of what you did with your pointer variable
无论你对指针变量做了什么,i == 2仍然是真的
Pointer variables could be seen or thought of as holding "special" integer values, they store a memory address, which usually is 32-bit number (unless you are running on a 64-bit address computer).
指针变量可以看作或被认为是持有“特殊”整数值,它们存储一个内存地址,通常是32位数(除非你在64位地址计算机上运行)。