C语言 使用函数更改指针包含的地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13431108/
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
Changing address contained by pointer using function
提问by pranphy
If I've declared a pointer pas int *p; in main module, I can change the address contained by pby assigning p=&a;where ais another integer variable already declared.
I now want to change the address by using a function as::
如果我已将指针声明p为int *p; 在主模块中,我可以p通过分配p=&a;wherea是另一个已经声明的整数变量来更改包含的地址。我现在想通过使用一个函数来更改地址:
void change_adrs(int*q)
{
int *newad;
q=newad;
}
If I call this function from main module
如果我从主模块调用这个函数
int main()
{
int *p;
int a=0;
p=&a; // this changes the address contained by pointer p
printf("\n The address is %u ",p);
change_adrs(p);
printf("\n the address is %u ",p); // but this doesn't change the address
return 0;
}
the address content is unchanged. What's wrong with using a function for same task?
地址内容不变。对同一任务使用函数有什么问题?
回答by mathematician1975
In C, functions arguments are passed by value. Thus a copy is made of your argument and the change is made to that copy, not the actual pointer that you are expecting to see modified. You will need to change your function to accept a double pointer argument and make the change to the dereferenced argument if you want to do this. For example
在 C 中,函数参数是按值传递的。因此,一个副本由您的参数组成,并且对该副本进行了更改,而不是您希望看到修改的实际指针。如果您想这样做,您将需要更改您的函数以接受双指针参数并对取消引用的参数进行更改。例如
void foo(int** p) {
*p = NULL; /* set pointer to null */
}
void foo2(int* p) {
p = NULL; /* makes copy of p and copy is set to null*/
}
int main() {
int* k;
foo2(k); /* k unchanged */
foo(&k); /* NOW k == NULL */
}
If you have the luxury of using C++ an alternative way would be to change the function to accept a reference to a pointer.
如果您有幸使用 C++,另一种方法是更改函数以接受对指针的引用。
回答by pranphy
In C, variables are passed by value - a copy of the pointer is passed to the function. Use another pointer to the pointer instead:
在 C 中,变量是按值传递的——指针的副本被传递给函数。改用另一个指向该指针的指针:
void change(int **p, int *someOtherAddress)
{
*p = someOtherAddress;
}
int a = 1, b = 2;
int *p = &a;
printf("*p = %d\n", *p);
change(&p, &b);
printf("*p = %d\n", *p);
This prints
这打印
*p = 1
*p = 2
回答by snr
If you want to alter the content of a variable in a function in C, pointer is a kinda variable as well, you have to pass it by pointeror indirect referenceby using always &address and *dereference operators. I mean *operator is always used and preceded when changing the value of a variable.
如果要在 C 中更改函数中变量的内容,指针也是一种变量,您必须使用始终地址和取消引用运算符通过指针或间接引用传递它。我的意思是在更改变量的值时总是使用运算符并放在前面。&**
#include <stdio.h>
#include <stdlib.h>
void changeIntVal(int *x) {
*x = 5;
}
void changePointerAddr(int **q) {
int *newad;
*q = newad;
}
void changePPAddr(int ***q) {
int **dummy;
*q = dummy;
}
int main() {
int *p;
int **pp;
int *tempForPP;
int a = 0;
printf("\n The address pointing by p -> %p, pp -> %p, value of a -> %d ", p, pp, a);
p = &a;
pp = &tempForPP;
printf("\n The address pointing by p -> %p, pp -> %p, value of a -> %d ", p, pp, a);
changeIntVal(&a); // ----
// |---
changePointerAddr(&p); // ---- |----> parts of what I mean
// |---
changePPAddr(&pp); // ----
printf("\n The address pointing by p -> %p, pp -> %p, value of a -> %d ", p, pp, a);
return 0;
}
回答by todkwxrtvwmzonunswam
For a primitive data type such as an int, the double pointers are not necessary. You can write directly into the address where the intis stored, treating its address as a pointer in the function being called. This is unlike a chararray ("string") where the size of what is pointed to is variable and you must therefore use another level of indirection when changing it from within a called function. Try this:
对于诸如 an 之类的原始数据类型int,双指针不是必需的。您可以直接写入int存储的地址,将其地址视为被调用函数中的指针。这与char数组(“字符串”)不同,数组(“字符串”)所指向的内容的大小是可变的,因此当从被调用函数中更改它时,您必须使用另一个间接级别。尝试这个:
void foo(int *oldVal)
{
int newVal = 99; // or whatever you want
*oldVal = newVal;
}
int main(int argc, char *argv[])
{
int someVal = 0;
foo(&someVal); // we send its address to foo()
printf("someVal is now %d.\n", someVal);
return EXIT_SUCCESS;
}
回答by Omkant
This won't change the actual value ofpbecause the qin function is local to that and change in that function will not reflect in mainso pass the address of pinstead of passing pby value
这不会改变 的实际值,p因为qin 函数是本地的,并且该函数中的更改不会反映在main因此传递的地址p而不是p按值传递
Use this syntax below
在下面使用此语法
void change_adrs(int **q)
{
int * otheraddess;
*q = otheraddress;
}
and call like this change_adrs(&p);
并像这样打电话 change_adrs(&p);
Or, you have other way around : change the return type of function and catch the returned address.
或者,您还有其他方法:更改函数的返回类型并捕获返回的地址。
int* change_adrs(int *q)
{
int * otheraddess;
q = otheraddress;
return q;
}
int main()
{
p=change_adrs(p);
return 0;
}

