简体   繁体   English

为什么我们需要 int *ptr = NULL;?

[英]Why do we need int *ptr = NULL;?

I don't quite understand why in this example we need a part int *ptr = NULL;我不太明白为什么在这个例子中我们需要一个部分int *ptr = NULL; . . I understand what pointers are, but why do we need this * (points at a value attached to an address) here?我明白什么是指针,但为什么我们需要这个 *(指向附加到地址的值)? And why do we need NULL here?为什么我们在这里需要 NULL?

#include <stdio.h>

int main() {
    int a[5] = {22, 33, 44, 55, 66};
    int *ptr = NULL;
    int i;

    ptr = a;
    for (i = 0; i < 5; i++) {
        printf("%d ", *(ptr + i));
    }
}

This intermediate initialization这个中间初始化

int *ptr = NULL;

is redundant.是多余的。 You could write at once你可以一次写

int *ptr = a;

Moreover such an initialization is a bad programming style.此外,这种初始化是一种糟糕的编程风格。 Each variable should be declared and initialized where it is used.每个变量都应该在使用它的地方声明和初始化。 Otherwise the code will confuses users as it confused you.否则代码会使用户感到困惑,因为它使您感到困惑。

I would rewrite the program the following way我会用以下方式重写程序

#include <stdio.h>

int main( void )
{
    int a[] = { 22, 33, 44, 55, 66 };
    const size_t N = sizeof( a ) / sizeof( *a );

    for ( const int *ptr = a; ptr != a + N; ++ptr ) 
    {
        printf( "%d ", *ptr );
    }

    putchar( '\n' );
}

There are cases when the same one pointer can be used in various contexts.在某些情况下,可以在各种上下文中使用相同的单指针。 In this case it makes sense to declare it and initialize with NULL.在这种情况下,声明它并用 NULL 初始化是有意义的。 But your simple program is not the case.但是您的简单程序并非如此。

在你的情况下int *ptr = NULL是完全多余的,因为你可以像 Vlad 已经说过int *ptr = a那样写int *ptr = a ,但更一般地说,初始化指向NULL指针是一个好主意,因为如果由于某种原因你有错误您的代码并且您需要调试它,如果指针以NULL开头,则调试指针发生的情况比未初始化它并且填充了一些垃圾值要容易得多。

I don't quite understand why in this example we need a part int *ptr = NULL;我不太明白为什么在这个例子中我们需要一个部分int *ptr = NULL; . .

[...] [...]

why do we need this * (points at a value attached to an address) here?为什么我们需要这个 *(指向附加到地址的值)?

You are mistaking the significance of the * .您误解了*的重要性。 In that context, it is not the unary * operator.在这种情况下,它不是一元*运算符。 Rather, it is part of the syntax for declaring a variable whose type is a pointer type.相反,它是声明类型为指针类型的变量的语法的一部分。 The declaration declares ptr as a variable of type int * , ie pointer-to-integer.该声明将ptr声明为int *类型的变量,指向整数的指针。 Without the * , the declaration would be designating ptr 's type as int , not as a pointer type.如果没有* ,声明会将ptr的类型指定为int ,而不是指针类型。

And why do we need NULL here?为什么我们在这里需要 NULL?

You don't.你没有。 The = NULL is an initializer for variable ptr , defining its initial value, but you assign a different value to that variable before ever reading it, so the initializer has no practical significance.= NULL是可变的初始化ptr ,确定它的初始值,但你可曾阅读之前分配一个不同的值,该变量,所以初始化没有实际意义。

Some people are inclined to provide such initializers as a matter of course, on the theory that it avoids accidental usage of wild pointers and / or allows (otherwise-)uninitialized pointers to be detected, but as Emerson wrote, "A foolish consistency is the hobgoblin of little minds."有些人倾向于提供这样的初始化器作为理所当然,理论上它可以避免意外使用野指针和/或允许检测(否则)未初始化的指针,但正如艾默生所写,“愚蠢的一致性是小头脑的大妖精。” The initializer serves no useful purpose in this particular code.初始化程序在此特定代码中没有任何用处。 If one insists on providing an initializer then there's no reason not to initialize ptr directly to a :如果坚持提供初始化程序,则没有理由不将ptr直接初始化为a

int *ptr = a;

as your other answers also suggest.正如您的其他答案所暗示的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM