Complete C newb here. Trying to learn/understand pointers by messing with simple code fragments.
完整的C新手。试图通过混乱简单的代码片段来学习/理解指针。
#include <stdio.h>
void swap(int *px, int *py)
{
int tmp;
tmp = *px;
*px = *py;
*py = tmp;
}
main()
{
int *a, *b;
*a = 1;
*b = 2;
swap(&a,&b);
printf("%d %d\n", *a, *b);
}
Why is this not valid? The code works when I remove the dereferencing operator *
from main.
为什么这是无效的?当我从main中删除取消引用操作符*时,代码就可以工作了。
Conceptually, this seems like it should work. I initialize a
and b
as pointers which point to int 1
and int 2
, respectively. I then send their addresses to swap()
, which should switch what they point to.
从概念上来说,这似乎是可行的。我将a和b初始化为指向int 1和int 2的指针。然后,我将它们的地址发送到swap(),交换器应该将它们指向的内容转换为swap()。
6 个解决方案
#1
11
There are a couple of problems. First, the pointers a
and b
are not pointing to valid memory. So the assignment of the integer values is undefined (possible crash). Secondly, the call to swap (assuming a and b are pointing to valid memory) should not include the address (it is currently sending the address of the pointer variable).
有几个问题。首先,指针a和b没有指向有效内存。所以整数值的赋值没有定义(可能会崩溃)。其次,对swap的调用(假设a和b指向有效内存)不应该包含地址(它正在发送指针变量的地址)。
The following changes would make it work:
以下的改变将使它起作用:
int a, b;
a = 1;
b = 2;
swap(&a,&b);
printf("%d %d\n", a, b);
#2
6
The swap()
function is OK but inside main you are taking the addresses of pointers, so you're passing int**
arguments to int*
parameters.
swap()函数是OK的,但是在main函数中,要获取指针的地址,所以要将int**参数传递给int*参数。
int *a, *b;
swap(&a,&b);
To fix it, replace the code in main()
with :
要修复它,请将main()中的代码替换为:
int a = 1, b = 2;
swap(&a,&b);
printf("%d %d\n", a, b);
#3
4
Pointers point to data. A pointer itself doesn't comprise memory for storage, it just points to existing memory. So when you declare int *a;
, you just have a garbage pointer with no useable value, and you mustn't dereference it.
指针指向的数据。指针本身并不包含用于存储的内存,它只是指向现有的内存。当你声明int *a时;,您只有一个没有可用值的垃圾指针,并且不应该取消引用。
The only sensible way to use pointers is to assign them the address-of something (or the result of some allocation function):
使用指针的唯一明智的方法是给它们指定一个地址(或者一些分配函数的结果):
int i;
int *a = &i; // now a points to i
Therefore, the right way to use your swap function is to pass it addresses of integers:
因此,使用交换函数的正确方法是传递整数的地址:
int i = 10;
int j = -2;
swap(&i, &j);
#4
2
a
and b
are uninitialized pointers, dereferencing them induces undefined behavior. You want:
a和b是未初始化的指针,取消它们的引用会导致未定义的行为。你想要的:
int main() {
int a, b;
a = 1;
b = 2;
swap(&a,&b);
printf("%d %d\n", a, b);
return 0;
}
#5
0
Your method signature is wrong. You ask for two pointers to int, yet you pass in two pointers to pointers to int.
您的方法签名是错误的。你要求两个指向int的指针,但你给两个指向int的指针。
#6
0
When you say, " I then send their addresses to swap(), which should switch what they point to." Are you trying to change the address values within the pointer variables in main, to switch which bit of memory they are pointing to? In that case you will need another step of redirection:
当你说,“我然后发送他们的地址交换(),这应该转换他们指向的。”您是否正在尝试更改main中指针变量中的地址值,以切换它们指向的内存?在这种情况下,您将需要进行另一步重定向:
#include <stdio.h>
void swap(int **px, int **py) {
int *tmp;
tmp = *px;
*px = *py;
*py = tmp;
}
int main (void) {
int x, y; /* storage to point to */
int *a, *b;
a = &x;
b = &y;
*a = 1;
*b = 2;
printf("(*a, *b, x, y) == (%d, %d, %d, %d)\n", *a, *b, x, y);
swap(&a, &b);
printf("(*a, *b, x, y) == (%d, %d, %d, %d)\n", *a, *b, x, y);
}
$ ./a.out
(*a, *b, x, y) == (1, 2, 1, 2)
(*a, *b, x, y) == (2, 1, 1, 2)
The x
& y
values have not changed, but a
was pointing to x
and now points to y
and vice versa for b
.
x和y的值没有变化,但是a指向x,现在指向y, b也指向y。
#1
11
There are a couple of problems. First, the pointers a
and b
are not pointing to valid memory. So the assignment of the integer values is undefined (possible crash). Secondly, the call to swap (assuming a and b are pointing to valid memory) should not include the address (it is currently sending the address of the pointer variable).
有几个问题。首先,指针a和b没有指向有效内存。所以整数值的赋值没有定义(可能会崩溃)。其次,对swap的调用(假设a和b指向有效内存)不应该包含地址(它正在发送指针变量的地址)。
The following changes would make it work:
以下的改变将使它起作用:
int a, b;
a = 1;
b = 2;
swap(&a,&b);
printf("%d %d\n", a, b);
#2
6
The swap()
function is OK but inside main you are taking the addresses of pointers, so you're passing int**
arguments to int*
parameters.
swap()函数是OK的,但是在main函数中,要获取指针的地址,所以要将int**参数传递给int*参数。
int *a, *b;
swap(&a,&b);
To fix it, replace the code in main()
with :
要修复它,请将main()中的代码替换为:
int a = 1, b = 2;
swap(&a,&b);
printf("%d %d\n", a, b);
#3
4
Pointers point to data. A pointer itself doesn't comprise memory for storage, it just points to existing memory. So when you declare int *a;
, you just have a garbage pointer with no useable value, and you mustn't dereference it.
指针指向的数据。指针本身并不包含用于存储的内存,它只是指向现有的内存。当你声明int *a时;,您只有一个没有可用值的垃圾指针,并且不应该取消引用。
The only sensible way to use pointers is to assign them the address-of something (or the result of some allocation function):
使用指针的唯一明智的方法是给它们指定一个地址(或者一些分配函数的结果):
int i;
int *a = &i; // now a points to i
Therefore, the right way to use your swap function is to pass it addresses of integers:
因此,使用交换函数的正确方法是传递整数的地址:
int i = 10;
int j = -2;
swap(&i, &j);
#4
2
a
and b
are uninitialized pointers, dereferencing them induces undefined behavior. You want:
a和b是未初始化的指针,取消它们的引用会导致未定义的行为。你想要的:
int main() {
int a, b;
a = 1;
b = 2;
swap(&a,&b);
printf("%d %d\n", a, b);
return 0;
}
#5
0
Your method signature is wrong. You ask for two pointers to int, yet you pass in two pointers to pointers to int.
您的方法签名是错误的。你要求两个指向int的指针,但你给两个指向int的指针。
#6
0
When you say, " I then send their addresses to swap(), which should switch what they point to." Are you trying to change the address values within the pointer variables in main, to switch which bit of memory they are pointing to? In that case you will need another step of redirection:
当你说,“我然后发送他们的地址交换(),这应该转换他们指向的。”您是否正在尝试更改main中指针变量中的地址值,以切换它们指向的内存?在这种情况下,您将需要进行另一步重定向:
#include <stdio.h>
void swap(int **px, int **py) {
int *tmp;
tmp = *px;
*px = *py;
*py = tmp;
}
int main (void) {
int x, y; /* storage to point to */
int *a, *b;
a = &x;
b = &y;
*a = 1;
*b = 2;
printf("(*a, *b, x, y) == (%d, %d, %d, %d)\n", *a, *b, x, y);
swap(&a, &b);
printf("(*a, *b, x, y) == (%d, %d, %d, %d)\n", *a, *b, x, y);
}
$ ./a.out
(*a, *b, x, y) == (1, 2, 1, 2)
(*a, *b, x, y) == (2, 1, 1, 2)
The x
& y
values have not changed, but a
was pointing to x
and now points to y
and vice versa for b
.
x和y的值没有变化,但是a指向x,现在指向y, b也指向y。