C语言怎么赋空值

Python08

C语言怎么赋空值,第1张

C语言并没有空值这个概念,一般习惯上,指针若是指向地址0便是空值,其他数据若内容是0便是空值。填充一大片内存区域可以使用memset函数,它的填充长度是按字节计。整形:int a = 0字符型:char a = 0数组:int a[20]memset( a, 0, sizeof(int)*20 )C字符串只需要把第一个字符设为'\0'即可。当然也可以全部清成0。char a[20] = "ashdfuih"memset( a, 0, sizeof(char)*20 )例如有一个结构体Some x,可以这样清零:memset( &x, 0, sizeof(Some) )如果是一个结构体的数组Some x[10],可以这样:menset( x, 0, sizeof(Some)*10 )任何一个指针都可以通过直接赋为0变成空指针:Some* p = 0

#include<stdio.h>

int main(int argc, char* argv[])

{

 //二维数组包含10个字符串

 char str_list[10][100]

 //依次把字符串置为空串,也就是将首位置为'/0'

 for(int i = 0 i < 10 ++i)

 {

  str_list[i][0] = '\0'

 }

 return 0

}

NULL在32位操作系统的内存中的表现就是0x0000 0000 0000 0000 0000 0000 0000 0000,即32位都为0,其值其实为0

所以,有

int i = 0//让i为NULL

char c = '\0'//'\0'在内存中的表现也为一串0,等价与0和NULL