这些例子(*和&)有什么不同?

时间:2023-01-24 00:07:30

I wanted to know what's the difference between these two versions of the main() code in C:

我想知道这两个版本的主()代码在C中的区别是什么:

int main() {
   uint32_t a;
   f(&a);
}

and

int main() {
   uint32_t *a;
   f(a);
}

for a function

为一个函数

void f(uint32_t *pointer) {
   // ...
}

4 个解决方案

#1


12  

In your first example, you pass a pointer to uninitialized variable a. f() could store a value there, for instance, and main() would be able to use that value later.

在第一个示例中,将指针传递给未初始化的变量a. f()可以在其中存储一个值,main()稍后将能够使用该值。

In your second example, you pass an uninitialized pointer a. f() can't do anything useful with it.

在第二个示例中,传递一个未初始化的指针a. f()不能使用它做任何有用的事情。

#2


4  

In the first version you pass a pointer to an un-initialized variable to f(). Don't, unless f()'s task is to initialize the variable.

在第一个版本中,将指向未初始化变量的指针传递给f()。除非f()的任务是初始化变量,否则不要这样做。

In the second version you pass an uninitialized pointer to function f(). Don't.

在第二个版本中,您将一个未初始化的指针传递给函数f()。不喜欢。

#3


2  

wee pointer questions!!!

凌晨指针问题! ! !

Ok so you know that the function f needs to take a pointer which is exactly as it sounds an address to that actual location in memory. For the first example(f(&a)) you have to pass the address because it lives inside the stack and really isn't shared anywhere outside the life of the function. so when the function returns the memory is destroyed and no longer available. the value of the pointer is not passed just the pointer to the value is passed. which can cause problems because if you change that value then all the things that "point to it" are now changed.

你知道函数f需要一个指针它听起来和内存中的地址一样。对于第一个示例(f(&a)),您必须传递这个地址,因为它位于堆栈中,并且在函数的生命之外的任何地方都没有共享。因此,当函数返回时,内存被破坏,不再可用。指针的值不是传递给值的指针而是传递给值的指针。这可能会导致问题,因为如果你改变了那个值,那么所有指向它的东西现在都改变了。

Now for the second one you get the memory from the heap or where ever but that stores the address of the value not the actual value so you can manipulate it and return nothing and the value is still there.

现在对于第二个,你可以从堆或者任何地方获得内存但是它存储了值的地址而不是实际的值所以你可以对它进行操作,什么都不返回值仍然在那里。

#4


2  

They are defining different types of variables, without initialization.

它们定义不同类型的变量,没有初始化。

uint32_t a; defines uint32_t variable on the stack and the function call will pass its address to the f() function.

uint32_t;在堆栈上定义uint32_t变量,函数调用将其地址传递给f()函数。

uint32_t *a; defines a pointer on the stack and pass its value to the function. The pointer is not initialized, thus it could point to any block and any attempt to access that address will result into undefined behavior.

uint32_t *;定义堆栈上的指针并将其值传递给函数。指针没有初始化,因此它可以指向任何块,任何访问该地址的尝试都会导致未定义的行为。

From the perspective of the f() function, it sees pointer values passed to it. In the first call, it can use that address, while in the second, it cannot.

从f()函数的角度来看,它看到传递给它的指针值。在第一个调用中,它可以使用这个地址,而在第二个调用中,它不能。

#1


12  

In your first example, you pass a pointer to uninitialized variable a. f() could store a value there, for instance, and main() would be able to use that value later.

在第一个示例中,将指针传递给未初始化的变量a. f()可以在其中存储一个值,main()稍后将能够使用该值。

In your second example, you pass an uninitialized pointer a. f() can't do anything useful with it.

在第二个示例中,传递一个未初始化的指针a. f()不能使用它做任何有用的事情。

#2


4  

In the first version you pass a pointer to an un-initialized variable to f(). Don't, unless f()'s task is to initialize the variable.

在第一个版本中,将指向未初始化变量的指针传递给f()。除非f()的任务是初始化变量,否则不要这样做。

In the second version you pass an uninitialized pointer to function f(). Don't.

在第二个版本中,您将一个未初始化的指针传递给函数f()。不喜欢。

#3


2  

wee pointer questions!!!

凌晨指针问题! ! !

Ok so you know that the function f needs to take a pointer which is exactly as it sounds an address to that actual location in memory. For the first example(f(&a)) you have to pass the address because it lives inside the stack and really isn't shared anywhere outside the life of the function. so when the function returns the memory is destroyed and no longer available. the value of the pointer is not passed just the pointer to the value is passed. which can cause problems because if you change that value then all the things that "point to it" are now changed.

你知道函数f需要一个指针它听起来和内存中的地址一样。对于第一个示例(f(&a)),您必须传递这个地址,因为它位于堆栈中,并且在函数的生命之外的任何地方都没有共享。因此,当函数返回时,内存被破坏,不再可用。指针的值不是传递给值的指针而是传递给值的指针。这可能会导致问题,因为如果你改变了那个值,那么所有指向它的东西现在都改变了。

Now for the second one you get the memory from the heap or where ever but that stores the address of the value not the actual value so you can manipulate it and return nothing and the value is still there.

现在对于第二个,你可以从堆或者任何地方获得内存但是它存储了值的地址而不是实际的值所以你可以对它进行操作,什么都不返回值仍然在那里。

#4


2  

They are defining different types of variables, without initialization.

它们定义不同类型的变量,没有初始化。

uint32_t a; defines uint32_t variable on the stack and the function call will pass its address to the f() function.

uint32_t;在堆栈上定义uint32_t变量,函数调用将其地址传递给f()函数。

uint32_t *a; defines a pointer on the stack and pass its value to the function. The pointer is not initialized, thus it could point to any block and any attempt to access that address will result into undefined behavior.

uint32_t *;定义堆栈上的指针并将其值传递给函数。指针没有初始化,因此它可以指向任何块,任何访问该地址的尝试都会导致未定义的行为。

From the perspective of the f() function, it sees pointer values passed to it. In the first call, it can use that address, while in the second, it cannot.

从f()函数的角度来看,它看到传递给它的指针值。在第一个调用中,它可以使用这个地址,而在第二个调用中,它不能。