www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - const char* or const(char)* when porting C headers?

reply "Gary Willoughby" <dev nomad.so> writes:
When porting C headers which include function declarations with 
using char* types. Is it best to use const char* or const(char)* 
as the type in the D declaration?
Dec 21 2013
parent reply Alexandr Druzhinin <drug2004 bk.ru> writes:
22.12.2013 07:47, Gary Willoughby пишет:
 When porting C headers which include function declarations with using
 char* types. Is it best to use const char* or const(char)* as the type
 in the D declaration?
C vs D const char* == const(char)* const char const* == const char*
Dec 21 2013
next sibling parent reply "Gary Willoughby" <dev nomad.so> writes:
On Sunday, 22 December 2013 at 04:06:05 UTC, Alexandr Druzhinin 
wrote:
 22.12.2013 07:47, Gary Willoughby пишет:
 When porting C headers which include function declarations 
 with using
 char* types. Is it best to use const char* or const(char)* as 
 the type
 in the D declaration?
C vs D const char* == const(char)* const char const* == const char*
Thanks, that makes sense. But how would i port this parameter: struct Tcl_Obj * CONST * objv Maybe like this?: const Tcl_Obj[]* objv
Dec 22 2013
parent reply "Gary Willoughby" <dev nomad.so> writes:
On Sunday, 22 December 2013 at 15:49:43 UTC, Gary Willoughby 
wrote:

 Thanks, that makes sense. But how would i port this parameter:
and these: CONST84 char **tablePtr = ? CONST84 char ***argvPtr = ?
Dec 22 2013
parent reply Benjamin Thaut <code benjamin-thaut.de> writes:
Am 22.12.2013 17:02, schrieb Gary Willoughby:
 On Sunday, 22 December 2013 at 15:49:43 UTC, Gary Willoughby wrote:

 Thanks, that makes sense. But how would i port this parameter:
and these: CONST84 char **tablePtr = ? CONST84 char ***argvPtr = ?
In C/C++ the const always applies to whatever is left of it. If there is nothing left of it, it applies to what is right to it. So "const char**" is equivalent to "char const **". That means the only part that is const is the char. In D const applies to whatever is inside the parantheses. So the equivalent in D would be const(char)** and const(char)*** If you have something like the following in C: const char * const the D equivalent would be const(char*) Note that the star is included in the parantheses here, because the pointer is const to, not only the data it points to. the D type: const(char**) would be equivalent to C: char const * const * const. (But you won't ever needs this) Kind Regards Benjamin Thaut
Dec 22 2013
parent reply "Gary Willoughby" <dev nomad.so> writes:
On Sunday, 22 December 2013 at 16:45:15 UTC, Benjamin Thaut wrote:
 Am 22.12.2013 17:02, schrieb Gary Willoughby:
 On Sunday, 22 December 2013 at 15:49:43 UTC, Gary Willoughby 
 wrote:

 Thanks, that makes sense. But how would i port this parameter:
and these: CONST84 char **tablePtr = ? CONST84 char ***argvPtr = ?
In C/C++ the const always applies to whatever is left of it. If there is nothing left of it, it applies to what is right to it. So "const char**" is equivalent to "char const **". That means the only part that is const is the char. In D const applies to whatever is inside the parantheses. So the equivalent in D would be const(char)** and const(char)*** If you have something like the following in C: const char * const the D equivalent would be const(char*) Note that the star is included in the parantheses here, because the pointer is const to, not only the data it points to. the D type: const(char**) would be equivalent to C: char const * const * const. (But you won't ever needs this) Kind Regards Benjamin Thaut
Ah right, so: struct Tcl_Obj * CONST * objv would be: const(Tcl_Obj*)* objv or const(Tcl_Obj*)[] objv
Dec 22 2013
parent reply Benjamin Thaut <code benjamin-thaut.de> writes:
Am 22.12.2013 18:39, schrieb Gary Willoughby:
 Ah right, so:

 struct Tcl_Obj * CONST * objv

 would be:

 const(Tcl_Obj*)* objv  or  const(Tcl_Obj*)[] objv
Yes
Dec 22 2013
parent reply "Gary Willoughby" <dev nomad.so> writes:
On Sunday, 22 December 2013 at 18:28:43 UTC, Benjamin Thaut wrote:
 Am 22.12.2013 18:39, schrieb Gary Willoughby:
 Ah right, so:

 struct Tcl_Obj * CONST * objv

 would be:

 const(Tcl_Obj*)* objv  or  const(Tcl_Obj*)[] objv
Yes
Great thanks! I thought i had a pretty good handle on C but porting some headers makes me scratch my head.
Dec 22 2013
parent Benjamin Thaut <code benjamin-thaut.de> writes:
Am 22.12.2013 20:34, schrieb Gary Willoughby:
 On Sunday, 22 December 2013 at 18:28:43 UTC, Benjamin Thaut wrote:
 Am 22.12.2013 18:39, schrieb Gary Willoughby:
 Ah right, so:

 struct Tcl_Obj * CONST * objv

 would be:

 const(Tcl_Obj*)* objv  or  const(Tcl_Obj*)[] objv
Yes
Great thanks! I thought i had a pretty good handle on C but porting some headers makes me scratch my head.
Yes, its sometimes really astonishing in what ways C-features can be abused. I very recently sumbled apon this: void (*callbackFunc)(GtkWidget* widget, void* userData); void registerCallback(callbackFunc func); void userCallback(GtkEntry* entry) { ... } void someFunc() { registerCallback((callbackFunc)&userCallback); } Note that the signature of the funciton does not match at all. The first parameter is "casted" automatically to a different data type, which only works because pointers always have the same size and the second paramter is omitted completely. This only works because of the C calling convetion. C can be a strange land ;-) Kind Regards Benjamin Thaut
Dec 22 2013
prev sibling parent Alexandr Druzhinin <drug2004 bk.ru> writes:
22.12.2013 11:06, Alexandr Druzhinin пишет:
 22.12.2013 07:47, Gary Willoughby пишет:
 When porting C headers which include function declarations with using
 char* types. Is it best to use const char* or const(char)* as the type
 in the D declaration?
C vs D const char* == const(char)* const char const* == const char*
Yes, the last line should be const char * const == const char* thanks to Benjamin IIRC in D qualificator is applied to the right part of statement if there is no the parantheses and to part inside the parantheses if they exists. Important thing is that in D qualificators are transitive. It makes type system more robust (from POV immutability), but doesn't complete it, sadly.
Dec 22 2013