存储指针值 [英] Store pointer value
问题描述
据我所知,当将指针传递给函数时,它仅成为真实指针的副本.现在,我希望更改实指针,而不必从函数返回指针.例如:
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);
另一件事,就是如何将内存分配给2个或多个维数组?不是通过下标,而是通过指针算术.是这样吗?
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;
与以下相同:
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;
为什么不:
allocateMemory(*a)
但是
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;
是包含实际值的内存地址,还是指针的内存地址?我一直认为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:
printf("Is this address of integer it is pointing to?%p\n",a);
printf("Is this address of the pointer itself?%p\n",&a);
推荐答案
我将尝试一次解决这些问题:
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);
这是
Here is a good explanation from the comp.lang.c FAQ.
另一件事,就是如何将内存分配给2个或多个维数组?
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);
同样,此处是
这是
与以下相同:
绝对不是.指针和数组是不同的.但是,您可以有时互换使用它们.从int array[2][3];
array[2][1] = 10;
int **array;
*(*(array+2)+1) = 10
ABSOLUTELY NOT. Pointers and arrays are different. You can sometimes use them interchangeably, however. Check out these questions from 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?
为什么不呢?
allocateMemory(*a)
有两件事-C没有传递引用,除非您自己通过传递指针来实现它,并且在这种情况下还因为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 a
isn'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?
这个问题对我来说真的没有意义,但我会尽力解释. a
(正确初始化时-在这里不是您的示例)是一个地址(指针本身). *a
是指向的对象-在这种情况下,它将是int
.
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). *a
is the object being pointed to - in this case that would be an int
.
顺便说一句,当打印这样的指针时:
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);
在两种情况下均正确.
这篇关于存储指针值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!