给指针赋值
例如,如果我有以下内容:
int *x;
x = func(a);
对于语句:x = func(a);
,我们是否说我们正在返回 x
的地址?或者说,我们究竟如何阅读它?
编辑:是否有资格说我们正在返回一个指向x
的指针?如果是这样,您能解释一下具体是如何完成的吗?我的意思是,我们如何返回一个指针?
If I have the following for example:
int *x;
x = func(a);
For the statement: x = func(a);
, do we say that we are returning an address to x
? Or, how exactly do we read it?
EDIT: Is it eligible to say that we are returning a pointer to x
? If so, can you explain how this is done exactly? I mean, how are we returning a pointer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
x
是一个指向int
的指针,换句话说,它是存储int
的内存位置的地址。所以x = func(a)
意味着func
返回int
的地址并将其存储在变量x
中>。注意不要返回局部变量的地址,其内容在
func
返回后将是未定义的。x
is a pointer to anint
, so in other words it is the address of a memory location where anint
is stored. Sox = func(a)
means thatfunc
returns the address of anint
and stores it in the variablex
.Take care not to return the address of a local variable whose contents would be undefined after
func
returns.回复:您的编辑:
是的,它当然有资格。尝试将指针视为任何其他数据类型。在底层,它们只是内存地址。可以像返回任何其他数据类型一样返回指针。
以这段代码为例:
假设“y”全局声明为:“int* y = ...”。现在“x”指向的内存地址与“y”指向的内存地址相同。
现在假设“y”没有声明为指针,而是声明为普通 int(例如“int y = 5”)。该函数可以做到这一点并且仍然有效:
RE: Your edit:
Yes, it is certainly eligible. Try and think of and treat pointers as any other data type. Under the hood, they are just memory addresses. A pointer can be returned the same way any other data type can be returned.
Take this code for example:
Say that "y" is declared globally as: "int* y = ...". Now the memory address being pointed to by "x" is the same as the memory address being pointed to by "y".
Now let's say that "y" was -not- declared as a pointer, but instead as a normal int (e.g. "int y = 5"). The function could do this and still be valid:
*x
指向内存中的 int 类型变量。所以函数func
应该返回int的地址。例如 getGlobalCounter 函数:
但删除从函数返回的对象并不总是好主意。在这种情况下,它应该会导致运行时错误,因为计数器没有像上面的示例那样动态分配
int
。*x
is points to int typed variable in memory. So functionfunc
should return address to int.For example the getGlobalCounter function:
But isn't always good idea to delete objects returned from functions. In that case it should result in runtime error, because of counter isn't dynamically allocated
int
as in top example.如果将变量的值分配给函数的返回类型,则该返回类型必须与变量的类型匹配。对于指针来说也是如此。
因此,如果您有:
并且...
然后将 myPointer 设置为等于 func() 会将“myPointer”指向的内存地址更改为 func() 返回的内存地址。
If you are assigning a variable's value to the return-type of a function, then that return-type must match the variable's type. This goes the same for pointers.
So, if you have:
And...
Then setting myPointer equal to func() will change the memory address which "myPointer" points to, to the memory address returned by func().
x 是一个指针,特别是指向 int 的指针。因此很明显,使用了两个内存位置。一个用于指针,一个用于其指向的内存(假设指针不为空)。
指针本身保存一个内存地址,特别是它所指向的内存的位置。类似 0xa456e4fa 的东西。
是的,func() 返回一个指针。 func 的原型如下所示。
请注意,返回类型是指向 int 的指针。从这里和我之前所说的,应该很明显这会返回什么。 0xyyyyyy 形式的东西,或者内存地址/指针。该内存地址进入您的 x 指针。
请记住,指针本身并不保存它所指向的数据,它只是地址。确实没有理由不能返回指针。但是,您确实需要注意退回的内容。您不想返回局部变量的地址,因为一旦函数完成执行,该变量就会超出范围。然后你将返回无效的地址。然而,返回本地指针的 VALUE 是可以的,因为返回的值(地址)将被保留,它指向的内存也将被保留。
我也刚刚意识到我给你写了一本书。该死,我累的时候肯定会胡言乱语。
x is a pointer, specifically to an int. So it should be obvious that there are two memory locations used. One for the pointer and one for the memory it's pointing to (assuming the pointer is not null).
The pointer itself holds a memory address, specifically the location of the memory it's pointing to. Something like 0xa456e4fa.
Yes, func() is returning a pointer. The prototype of func would look like the following..
Notice the return type is a pointer to an int. From this and what I said previously, it should be obvious what this will return. Something of the form 0xyyyyyy, or a memory address/pointer. That memory address goes into your x pointer.
Remember that the pointer itself is not holding the data that it's pointing to, it is only the address. There's really no reason you CAN'T return a pointer. However, you do need to be careful in WHAT you return. You do not want to return the address of a local variable because that variable will go out of scope once the function has completed its execution. You'll then be returning the address of something invalid. Returning the VALUE of a local pointer however, is fine because the value you returned (the address) will be preserved as will the memory it's pointing to.
I also just realized I wrote you a book. Damn, I sure do ramble when I'm tired.
该语句仅读取 x 被赋予函数
func
返回的值。为了使代码编译没有错误,func 应该返回一个地址。为了让代码按照AlexFZ
指出的那样执行,您应该注意func
不会返回函数本地变量的地址。The statement just reads that x is assigned the value returned by function
func
. For the code to compile without errors , the func should return an address though . And for the code to execute as expected asAlexFZ
pointed out , you should take care thatfunc
does not return the address of a variable local to the function.这意味着 func() 返回一个 int* 类型的变量。
不,我们返回一个指向整数的指针并将其分配给 x,因为它是一个指针。
这是返回 int 指针的示例代码
int* func(int a)
{
int *y = &a;
返回y;
}
It means that func() returns a variable of type int*.
No, we are returning a pointer to an integer and assigning it to x since it is a pointer.
This is an example code which returns an int pointer
int* func(int a)
{
int *y = &a;
return y;
}