给指针赋值

发布于 2024-11-25 14:09:07 字数 263 浏览 3 评论 0原文

例如,如果我有以下内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梦与时光遇 2024-12-02 14:09:07

x 是一个指向 int 的指针,换句话说,它是存储 int 的内存位置的地址。所以 x = func(a) 意味着 func 返回 int 的地址并将其存储在变量 x 中>。

注意不要返回局部变量的地址,其内容在 func 返回后将是未定义的。

x is a pointer to an int, so in other words it is the address of a memory location where an int is stored. So x = func(a) means that func returns the address of an int and stores it in the variable x.

Take care not to return the address of a local variable whose contents would be undefined after func returns.

永言不败 2024-12-02 14:09:07

回复:您的编辑:

编辑:是否有资格说我们正在返回指向 x 的指针?如果是这样,您能解释一下这是如何完成的吗?我的意思是,我们如何返回一个指针?

是的,它当然有资格。尝试将指针视为任何其他数据类型。在底层,它们只是内存地址。可以像返回任何其他数据类型一样返回指针。

以这段代码为例:

int* giveMeAPointer() {
  return y;
}
int* x = giveMeAPointer();

假设“y”全局声明为:“int* y = ...”。现在“x”指向的内存地址与“y”指向的内存地址相同。

现在假设“y”没有声明为指针,而是声明为普通 int(例如“int y = 5”)。该函数可以做到这一点并且仍然有效:

int* giveMeAPointer() {
  int* result = &y;
  return result;
}

RE: Your edit:

EDIT: Is it eligible to say that we are returning a pointer to x? If so, can you explain how is this done exactly? I mean, how are we returning a pointer?

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:

int* giveMeAPointer() {
  return y;
}
int* x = giveMeAPointer();

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:

int* giveMeAPointer() {
  int* result = &y;
  return result;
}
喜爱纠缠 2024-12-02 14:09:07

*x 指向内存中的 int 类型变量。所以函数func应该返回int的地址。

int *x;
x = new int;          // create your own int 
*x = 5;               // access - set it's value
delete x;             // delete int - free memory
x = getGlobalCounter();
(*x)++;               // increment global pointer

例如 getGlobalCounter 函数:

static int counter;
int *getGlobalCounter() {
   return &counter;   // return address of counter
}

但删除从函数返回的对象并不总是好主意。在这种情况下,它应该会导致运行时错误,因为计数器没有像上面的示例那样动态分配 int

*x is points to int typed variable in memory. So function func should return address to int.

int *x;
x = new int;          // create your own int 
*x = 5;               // access - set it's value
delete x;             // delete int - free memory
x = getGlobalCounter();
(*x)++;               // increment global pointer

For example the getGlobalCounter function:

static int counter;
int *getGlobalCounter() {
   return &counter;   // return address of counter
}

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.

挽容 2024-12-02 14:09:07

如果将变量的值分配给函数的返回类型,则该返回类型必须与变量的类型匹配。对于指针来说也是如此。

因此,如果您有:

int* myPointer;

并且...

int* func();

然后将 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:

int* myPointer;

And...

int* func();

Then setting myPointer equal to func() will change the memory address which "myPointer" points to, to the memory address returned by func().

魂牵梦绕锁你心扉 2024-12-02 14:09:07

x 是一个指针,特别是指向 int 的指针。因此很明显,使用了两个内存位置。一个用于指针,一个用于其指向的内存(假设指针不为空)。

指针本身保存一个内存地址,特别是它所指向的内存的位置。类似 0xa456e4fa 的东西。

是的,func() 返回一个指针。 func 的原型如下所示。

int * func(someObj a); //I don't know the datatype of your parameter,
                       //so I'm just making something up.

请注意,返回类型是指向 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..

int * func(someObj a); //I don't know the datatype of your parameter,
                       //so I'm just making something up.

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.

空城仅有旧梦在 2024-12-02 14:09:07

该语句仅读取 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 as AlexFZ pointed out , you should take care that func does not return the address of a variable local to the function.

懒的傷心 2024-12-02 14:09:07

这意味着 func() 返回一个 int* 类型的变量。

是否有资格说我们正在返回一个指向 x 的指针?

不,我们返回一个指向整数的指针并将其分配给 x,因为它是一个指针。

这是返回 int 指针的示例代码
int* func(int a)
{
int *y = &a;
返回y;
}

It means that func() returns a variable of type int*.

Is it eligible to say that we are returning a pointer to x?

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;
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文