Thread overview | ||||||
---|---|---|---|---|---|---|
|
December 31, 2014 Checking C return results against NULL | ||||
---|---|---|---|---|
| ||||
Am I missing a more agreeable way to check the return value of a C function against NULL. It's fine if it's a char*, but if it returns a pointer to some kind of struct, one has to go through and convert each instance of NULL to a cast of the appropriate return type. Eg cast(funnystruct*)0 if ((tomime_fields.fld_description==cast(char*)0)) throw new Exception("show_part_info: description is NULL"); |
December 31, 2014 Re: Checking C return results against NULL | ||||
---|---|---|---|---|
| ||||
Posted in reply to Laeeth Isharc | On 1/01/2015 12:22 a.m., Laeeth Isharc wrote:
> Am I missing a more agreeable way to check the return value of a C
> function against NULL. It's fine if it's a char*, but if it returns a
> pointer to some kind of struct, one has to go through and convert each
> instance of NULL to a cast of the appropriate return type. Eg
> cast(funnystruct*)0
>
> if ((tomime_fields.fld_description==cast(char*)0))
> throw new Exception("show_part_info: description is NULL");
== for value
is for pointers
if (tomime_fields.fld_description is null)
throw new Exception("...");
|
December 31, 2014 Re: Checking C return results against NULL | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rikki Cattermole | On 12/31/2014 8:29 PM, Rikki Cattermole wrote: > On 1/01/2015 12:22 a.m., Laeeth Isharc wrote: >> Am I missing a more agreeable way to check the return value of a C >> function against NULL. It's fine if it's a char*, but if it returns a >> pointer to some kind of struct, one has to go through and convert each >> instance of NULL to a cast of the appropriate return type. Eg >> cast(funnystruct*)0 You can take your pick of these: if( !ptr ) ... if( ptr == null ) ... if( ptr is null ) ... >> >> if ((tomime_fields.fld_description==cast(char*)0)) >> throw new Exception("show_part_info: description is NULL"); > > == for value > is for pointers > > if (tomime_fields.fld_description is null) > throw new Exception("..."); Actually, while 'is' does work for pointers, it's only necessary for references. |
December 31, 2014 Re: Checking C return results against NULL | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | Thanks for the help. Laeeth |
Copyright © 1999-2021 by the D Language Foundation