c语言中的null_如何在C中使用NULL

c语言中的null

Several programming languages make use of the concept of null.

几种编程语言都使用null的概念。

Go has nil, JavaScript has null, Python has None, and so on.

Go具有nil ,JavaScript具有null ,Python具有None ,依此类推。

C has NULL.

C具有NULL

NULL however is used differently from other languages. In C, NULL is limited to identifying a null pointer.

但是,NULL与其他语言的用法不同。 在C中,NULL限于标识空指针

When we initialize a pointer, we might not always know what it points to. That’s when it is useful:

当我们初始化一个指针时,我们可能并不总是知道它指向什么。 那是有用的时候:

int * p_some_variable = NULL;

NULL is not available by default: you need to include stdio.h to use it (or if you prefer, stddef.h:

NULL默认情况下不可用:您需要包括stdio.h才能使用它(或者,如果您愿意,可以使用stddef.h

#include <stdio.h>

int main(void) {
  int * p_some_variable = NULL;
}

Otherwise the C compiler will give you an error:

否则,C编译器会给您一个错误:

hello.c:3:26: error: use of undeclared identifier
      'NULL'
        int * p_some_variable = NULL;
                                ^
1 error generated.

You can check if a pointer is a null pointer by comparing it to NULL:

您可以通过将其与NULL比较来检查指针是否为空指针:

#include <stdio.h>

int main(void) {
  int * p_some_variable = NULL;

  if (p_some_variable == NULL) {
    printf("equal");
  }
}

In practice, NULL is a constant equivalent to 0, or "\0".

实际上, NULL是等于0"\0"的常数。

This is why you can set a string to NULL using:

这就是为什么可以使用以下命令将字符串设置为NULL的原因:

char *a_string = '\0';

翻译自: https://flaviocopes.com/c-null/

c语言中的null

  • 8
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值