Domanda

I have a function with the following prototype void init(int *argc,char**argv); but in a subsequent call of this function in my program I don't need or even have need to provide such arguments. So I make a variable called int_null(oh,bad name,I know) and pass to function init(&int_null, NULL) but come to my mind that NULL has-zero value by standard and I could use it instead of int_null variable(just for avoid create this variable), as the folloing init(NULL,NULL) it works fine. But can I have some problem? some implementations define NULL as (void*)0 or 0or 0L and I'm sure if it can be a problem. The function needs to understand that argc is zero.

È stato utile?

Soluzione

char **bla1 = (void *) 0;
char **bla2 = 0;
char **bla3 = 0L;

All these assignments (initialization is done as per assigment) are valid in C and equivalent.

You can pass always NULL if the expected argument is of type int * or char **.

As @R.. added in his comment, this last sentence is true but with the exceptions of functions that accept a variable number of arguments and functions defined in the prototyped (old-style) syntax. In both these cases a cast would be required.

Altri suggerimenti

NULL should work; both 0 and (void *)0 can be assigned to any pointer type in C.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top