Search code examples
cpointersnullfunction-prototypes

Can I pass a NULL where int* is expected?


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.


Solution

  • 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.