C语言 存储指针值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4660321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Store pointer value
提问by Amumu
As I know, when a pointer is passed into a function, it becomes merely a copy of the real pointer. Now, I want the real pointer to be changed without having to return a pointer from a function. For example:
据我所知,当一个指针被传递给一个函数时,它只是一个真实指针的副本。现在,我希望更改真正的指针而不必从函数返回指针。例如:
int *ptr;
void allocateMemory(int *pointer)
{
pointer = malloc(sizeof(int));
}
allocateMemory(ptr);
Another thing, which is, how can I allocate memory to 2 or more dimensional arrays? Not by subscript, but by pointer arithmetic. Is this:
另一件事,即如何将内存分配给二维或更多维数组?不是通过下标,而是通过指针算法。这是:
int array[2][3];
array[2][1] = 10;
the same as:
等同于:
int **array;
*(*(array+2)+1) = 10
Also, why do I have to pass in the memory address of a pointer to a function, not the actual pointer itself. For example:
另外,为什么我必须传入指向函数的指针的内存地址,而不是实际的指针本身。例如:
int *a;
整数 *a;
why not:
为什么不:
allocateMemory(*a)
but
但
allocateMemory(a)
I know I always have to do this, but I really don't understand why. Please explain to me.
我知道我总是必须这样做,但我真的不明白为什么。请给我解释一下。
The last thing is, in a pointer like this:
最后一件事是,在这样的指针中:
int *a;
Is a the address of the memory containing the actual value, or the memory address of the pointer? I always think a is the memory address of the actual value it is pointing, but I am not sure about this. By the way, when printing such pointer like this:
a 是包含实际值的内存地址,还是指针的内存地址?我一直认为 a 是它指向的实际值的内存地址,但我不确定这一点。顺便说一句,当打印这样的指针时:
printf("Is this address of integer it is pointing to?%p\n",a);
printf("Is this address of the pointer itself?%p\n",&a);
回答by Carl Norum
I'll try to tackle these one at a time:
我会尝试一次解决这些问题:
Now, I want the real pointer to be changed without having to return a pointer from a function.
You need to use one more layer of indirection:
int *ptr; void allocateMemory(int **pointer) { *pointer = malloc(sizeof(int)); } allocateMemory(&ptr);Another thing, which is, how can I allocate memory to 2 or more dimensional arrays?
One allocation for the first dimension, and then a loop of allocations for the other dimension:
int **x = malloc(sizeof(int *) * 2); for (i = 0; i < 2; i++) x[i] = malloc(sizeof(int) * 3);Again, hereis link to this exact question from the comp.lang.c FAQ.
Is this:
int array[2][3]; array[2][1] = 10;the same as:
int **array; *(*(array+2)+1) = 10ABSOLUTELY NOT.Pointers and arrays are different. You can sometimesuse them interchangeably, however. Check out these questionsfrom the comp.lang.c FAQ.
Also, why do I have to pass in the memory address of a pointer to a function, not the actual pointer itself?
why not:
allocateMemory(*a)It's two things - C doesn't have pass-by-reference, except where you implement it yourself by passing pointers, and in this case also because
aisn't initialized yet - if you were to dereference it, you would cause undefined behaviour. This problem is a similar case to this one, found in the comp.lang.c FAQ.int *a;Is a the address of the memory containing the actual value, or the memory address of the pointer?
That question doesn't really make sense to me, but I'll try to explain.
a(when correctly initialized - your example here is not) is an address (the pointer itself).*ais the object being pointed to - in this case that would be anint.By the way, when printing such pointer like this:
printf("Is this address of integer it is pointing to?%p\n",a); printf("Is this address of the pointer itself?%p\n",&a);Correct in both cases.
现在,我希望更改真正的指针而不必从函数返回指针。
您需要再使用一层间接:
int *ptr; void allocateMemory(int **pointer) { *pointer = malloc(sizeof(int)); } allocateMemory(&ptr);另一件事,即如何将内存分配给二维或更多维数组?
第一个维度的一个分配,然后另一个维度的分配循环:
int **x = malloc(sizeof(int *) * 2); for (i = 0; i < 2; i++) x[i] = malloc(sizeof(int) * 3);同样,这里是来自comp.lang.c FAQ 的这个确切问题的链接。
这是:
int array[2][3]; array[2][1] = 10;等同于:
int **array; *(*(array+2)+1) = 10绝对不。指针和数组是不同的。但是,您有时可以互换使用它们。从comp.lang.c 常见问题解答中查看这些问题。
另外,为什么我必须传入指向函数的指针的内存地址,而不是实际的指针本身?
为什么不:
allocateMemory(*a)这是两件事 - C 没有传递引用,除非你通过传递指针自己实现它,在这种情况下也是因为
a尚未初始化 - 如果你要取消引用它,你会导致未定义的行为。这个问题是一个类似的情况这一项,在发现comp.lang.c常见问题解答。int *a;a 是包含实际值的内存地址,还是指针的内存地址?
这个问题对我来说真的没有意义,但我会尽力解释。
a(正确初始化时 - 您的示例不是)是一个地址(指针本身)。*a是指向的对象 - 在这种情况下,将是int.顺便说一句,当打印这样的指针时:
printf("Is this address of integer it is pointing to?%p\n",a); printf("Is this address of the pointer itself?%p\n",&a);在这两种情况下都正确。
回答by SLaks
To answer your first question, you need to pass a pointer to a pointer. (int**)
要回答您的第一个问题,您需要将指针传递给指针。( int**)
To answer your second question, you can use that syntax to access a location in an existing array.
However, a nested array (int[][]) is not the same as a pointer to a pointer (int**)
要回答第二个问题,您可以使用该语法访问现有数组中的位置。
但是,嵌套数组 ( int[][]) 与指向指针 ( int**)的指针不同
To answer your third question:
回答你的第三个问题:
Writing apasses the value of the variable a, which is a memory address.
Writing *apasses the value pointed toby the variable, which is an actual value, not a memory address.
写入a传递变量的值a,它是一个内存地址。
写入*a传递的是变量指向的值,它是一个实际值,而不是内存地址。
If the function takes a pointer, that means it wants an address, not a value.
Therefore, you need to pass a, not *a.
Had abeen a pointer to a pointer (int**), you would pass *a, not **a.
如果函数接受一个指针,这意味着它需要一个地址,而不是一个值。
因此,您需要通过a,而不是*a。
本来a是一个指向指针(int**)的指针,你会通过*a,而不是**a。
回答by Eric Chai
Your first question:
你的第一个问题:
you could pass a pointer's address:
你可以传递一个指针的地址:
void allocateMemory(int **pointer) {
*pointer = malloc(sizeof(int));
}
int *ptr;
allocateMemory(&ptr);
or you can return a pointer value:
或者你可以返回一个指针值:
int *allocateMemory() {
return malloc(sizeof(int));
}
int *ptr = mallocateMemory();
回答by filipe
I think you're a little confused about what a pointer actually is.
A pointer is just variable whose value represents an address in memory. So when we say that int *pis pointer to an integer, that just means pis a variable that holds a number that is the memory address of an int.
If you want a function to allocate a buffer of integers and change the value in the variable p, that function needs to know where in memory pis stored. So you have to give it a pointer to p(i.e., the memory address of p), which itself is a pointer to an integer, so what the function needs is a pointer to a pointer to an integer (i.e., a memory address where the function should store a number, which in turn is the memory address of the integers the function allocated), so
我认为您对指针实际上是什么有点困惑。
指针只是变量,其值表示内存中的地址。因此,当我们说它int *p是指向整数的指针时,这只是意味着它p是一个变量,其中包含一个数字,该数字是一个 的内存地址int。
如果您希望函数分配整数缓冲区并更改变量中的值p,则该函数需要知道内存p中的存储位置。所以你必须给它一个指向p(即p),它本身是一个整数指针,所以函数需要的是一个指向整数指针的指针(即函数应该存储一个数字的内存地址,它反过来是整数的内存地址分配的函数),所以
void allocateIntBuffer(int **pp)
{
// by doing "*pp = whatever" you're telling the compiler to store
// "whatever" not in the pp variable but in the memory address that
// the pp variable is holding.
*pp = malloc(...);
}
// call it like
int *p;
allocateIntBuffer(&p);
I think the key to your questions is to understand that there is nothing special about pointer variables. A pointer is a variable like any other, only that the value stored in that variable is used to represent a position in memory.
我认为您问题的关键是要了解指针变量没有什么特别之处。指针与其他任何变量一样是一个变量,只是存储在该变量中的值用于表示内存中的位置。
回答by R.. GitHub STOP HELPING ICE
Note that returning a pointer or forcing the caller to move the pointer in an out of a void *temp variable is the onlyway you can make use of the void *type to allow your function to work with different pointer types. char **, int **, etc. are not convertible to void **. As such, I would advise against what you're trying to do, and instead use the return value for functions that need to update a pointer, unless your function by design only works with a specific type. In particular, simple mallocwrappers that try to change the interface to pass pointer-to-pointer types are inherently broken.
请注意,返回指针或强制调用者将指针移出void *临时变量是您可以使用该类型以允许您的函数使用不同指针类型的唯一方法void *。char **、int **等不能转换为void **。因此,我建议您不要尝试这样做,而是使用需要更新指针的函数的返回值,除非您的函数设计为仅适用于特定类型。特别是,malloc尝试更改接口以传递指针到指针类型的简单包装器本质上是被破坏的。

