C语言 将数组元素初始化为 NULL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13293226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:22:40  来源:igfitidea点击:

initializing array element to NULL

carraysnull

提问by Nazerke

How to initialise an element of array to NULL.For example if I have char *array[10]; I want last element to be NULL, so that I can pass this array to execv

如何将数组元素初始化为 NULL。例如,如果我有 char *array[10]; 我希望最后一个元素为 NULL,以便我可以将此数组传递给 execv

回答by hmjd

To initialise an array of char*to all NULLs:

将一个数组初始化char*为所有NULLs:

char* array[10] = { NULL }; /* The remaining elements are implicitly NULL. */

If you want to provide initial elements for execv():

如果要为 提供初始元素execv()

char* array[10] = { "/usr/bin/ls", "-l" }; /* Again, remaining elements NULL. */

or if you wish to omit the dimension from the array declaration:

或者,如果您希望从数组声明中省略维度:

char* array[] = { "/usr/bin/ls", "-l", NULL };

回答by Omkant

NULLis nothing but : #define NULL (void*) 0 ULThe NULLyou are talking about is nulcharacter which is '\0'

NULL无非是:#define NULL (void*) 0 ULNULL你所谈论的是nul性格是'\0'

see man execvpage or other exec processes .. it's actually take variable number of arguments

请参阅man execv页面或其他 exec 进程..它实际上采用可变数量的参数

回答by user93353

execv takes an array of char *, not an array of char.

execv 采用 的数组char *,而不是 的数组char

char *const parms[] = {"abc", "-a", "pqr", NULL};

回答by Oldemiro Henry Williams Baloi

IF your array is object,String,Char if you declare it will be automatically null

如果你的数组是 object,String,Char 如果你声明它会自动为空

eg.

例如。

Oject [] myarray=new Object[10];

at any place of array is null

在数组的任何地方为空