C++ 中是否必须初始化指针?

发布于 2024-10-25 05:44:29 字数 170 浏览 8 评论 0原文

在给t赋值之前,是否必须在下面的代码中初始化t?代码正确吗?

void swap(int *x, int *y)
{
    int *t;
    *t = *x;
    *x = *y;
    *y = *t;
}

Is it compulsory to initialize t in the following code, before assigning value to t? Is the code correct?

void swap(int *x, int *y)
{
    int *t;
    *t = *x;
    *x = *y;
    *y = *t;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

乙白 2024-11-01 05:44:29

您不需要以指针开头:

void swap(int *x,int *y)
{
    int t; //not a pointer!
    t=*x;
    *x=*y;
    *y=t;
}
int a = 10, b = 20;
swap( &a, &b); //<-----------note : Needed &

--

或者,您可能需要以下交换函数:

void swap(int & x,int & y) //parameters are references now!
{
    int t; //not a pointer!
    t=x;
    x=y;
    y=t;
}
int a = 10, b = 20;
swap(a,b); //<----------- Note: Not needed & anymore!

You don't need pointer to begin with:

void swap(int *x,int *y)
{
    int t; //not a pointer!
    t=*x;
    *x=*y;
    *y=t;
}
int a = 10, b = 20;
swap( &a, &b); //<-----------note : Needed &

--

Or maybe, you want the following swap function:

void swap(int & x,int & y) //parameters are references now!
{
    int t; //not a pointer!
    t=x;
    x=y;
    y=t;
}
int a = 10, b = 20;
swap(a,b); //<----------- Note: Not needed & anymore!
偏闹i 2024-11-01 05:44:29

以下代码正确吗?

不!您的代码调用未定义的行为,因为您试图取消引用野指针。

 int *t;
 *t=*x; // bad

试试这个

 int t; // a pointer is not needed here
 t=*x; 

或者这个

int *t = x; // initialize the pointer

is the following section of code correct?

Nopes! Your code invokes Undefined behaviour because you are trying to dereference a wild pointer.

 int *t;
 *t=*x; // bad

Try this rather

 int t; // a pointer is not needed here
 t=*x; 

or this

int *t = x; // initialize the pointer

人间不值得 2024-11-01 05:44:29

该代码包含未定义的行为:

int *t;
*t=*x; // where will the value be copied?

除此之外它没有任何意义 - 您需要一个临时变量来存储值,而不是指针。

int t; // not a pointer
t=*x;
*x=*y;
*y=t;

That code contains undefined behavior:

int *t;
*t=*x; // where will the value be copied?

Besides that it makes no sense - you need a temporary variable to store the value, not the pointer.

int t; // not a pointer
t=*x;
*x=*y;
*y=t;
静谧 2024-11-01 05:44:29

对于指针来说这是正确的。

只有引用需要在声明时初始化(或在实例成员的构造函数中)。

编辑:但是您的代码中有错误,您不应该取消引用您的参数(即 int *ptr = otherPtr; 很好,而不是 int *ptr = *otherPtr;)

It's correct for a pointer.

Only references need to be initialized upon declaration (or in a constructor for instance members).

EDIT: but you got errors in your code, you shouldn't be dereferencing your parameters (ie int *ptr = otherPtr; is fine, not int *ptr = *otherPtr;)

逆夏时光 2024-11-01 05:44:29

如果您只是想让指针指向已经初始化的数据,那么您不需要初始化它。不过,您的做法是,您想要使用 malloc 函数之一为整数分配足够的堆空间。

在 C/C++ 中进行交换的正确、有效的方法是

void swap(int *x, int *y) {
    int *t = x;
    x = y;
    y = t;
}

If you just want to make your pointer point to already-initialized data, then you don't need to initialize it. The way you do it, though, yes, you want to use one of the malloc functions to allocate enough heap space for an integer.

The proper, efficient way, to do swapping in C/C++ is

void swap(int *x, int *y) {
    int *t = x;
    x = y;
    y = t;
}
骷髅 2024-11-01 05:44:29

您可以在此处找到执行此操作的正确方法

#include <stdio.h>

void swap(int *i, int *j)
{
    int t;
    t = *i;
    *i = *j;
    *j = t;
}

基本上,sharptooth已向您解释了原因,但是在那里,您将找到一些有关进行此类交换时后台发生的情况的更多详细信息和解释。希望它有助于理清您的想法。

You can find the right way of doing this here

#include <stdio.h>

void swap(int *i, int *j)
{
    int t;
    t = *i;
    *i = *j;
    *j = t;
}

Basically the reason has been explained to you by sharptooth but there you will find some more details and explanations about what happens in background when you do such a swap. Hope it helps to clear your ideas.

于我来说 2024-11-01 05:44:29
int *t;
*t=*x;

t 未指向任何能够取消引用的有效位置。

在给指针 t 赋值之前是否必须初始化 , 。

是的,初始化/分配以指向有效的内存位置。否则它会指向哪里。它可能会指向垃圾并导致取消引用上的未定义行为

int *t;
*t=*x;

t is not pointing to any valid location to be able to dereference.

is it compulsory to initialize , before assigning value to pointer t.

Yes, initializing / assigning to point to a valid memory location. Else where would it point to. It might some point to garbage and lead to undefined behavior on dereferencing.

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