在C的int指针的初始化 [英] Initialization of int pointer in C

查看:119
本文介绍了在C的int指针的初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于C.初始化一个int指针我的 刚刚通知 <一个非常简单的问题/ EM> 是:

A very simple question regarding initializing an int pointer in C. I was just informed that:

int *varname = {0};  

是无效的。

我还没有能够找到明确的参考,它指向了这一点,但有信心(根据评议代表),它可能是无效的,即使它编译,生成和释放calloc / malloc的声明好吗接受记忆。

I have not yet been able to find the explicit reference that points this out, but have confidence (based on commenters rep) that it probably is not valid, Even though it compiles, builds and accepts memory from calloc/malloc statements okay.

可有人请指出为什么上面的前pression是无效的?

Can someone please point out the specifics of why the above expression is not valid?

推荐答案

int *varname = {0};

只是等同于:

int *varname = 0;

这是 NULL 的有效替代类对象宏之一(作为 0 整型常量或如恒铸造到无效* 键入)。

It's one of valid replacements of NULL object-like macro (either as 0 integer constant or such constant casted to void * type).

N1570(C11草案),6.3.2.3/3:

N1570 (C11 draft), 6.3.2.3/3:

这是整型常量前pression值为0,或者这样的前pression
  投键入无效* ,被称为的空指针常量的如果空
  指针常数转换为指针类型,将所得
  指针,叫做的空指针的,是保证比较不等于一
  指向任何对象或函数。

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

示例( http://ideone.com/9917zk ):

#include <stdio.h>

int main(void)
{
    int *varname = {0};

    printf("%p\n", (void *) varname);

    return 0;
}

它的输出就是:

(nil)

(作为奖金的一部分注意,词是由的阿尔弗雷德·塔斯基

这篇关于在C的int指针的初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆