digitalmars.D.learn - const char* or const(char)* when porting C headers?
- Gary Willoughby (3/3) Dec 21 2013 When porting C headers which include function declarations with
- Alexandr Druzhinin (4/7) Dec 21 2013 C vs D
- Gary Willoughby (6/15) Dec 22 2013 Thanks, that makes sense. But how would i port this parameter:
- Gary Willoughby (5/6) Dec 22 2013 and these:
- Benjamin Thaut (19/24) Dec 22 2013 In C/C++ the const always applies to whatever is left of it. If there is...
- Gary Willoughby (5/33) Dec 22 2013 Ah right, so:
- Benjamin Thaut (2/6) Dec 22 2013 Yes
- Gary Willoughby (3/12) Dec 22 2013 Great thanks! I thought i had a pretty good handle on C but
- Benjamin Thaut (20/33) Dec 22 2013 Yes, its sometimes really astonishing in what ways C-features can be
- Alexandr Druzhinin (9/16) Dec 22 2013 Yes, the last line should be
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
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
On Sunday, 22 December 2013 at 04:06:05 UTC, Alexandr Druzhinin wrote:22.12.2013 07:47, Gary Willoughby пишет:Thanks, that makes sense. But how would i port this parameter: struct Tcl_Obj * CONST * objv Maybe like this?: const Tcl_Obj[]* objvWhen 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 22 2013
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
Am 22.12.2013 17:02, schrieb Gary Willoughby:On Sunday, 22 December 2013 at 15:49:43 UTC, Gary Willoughby wrote: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 ThautThanks, that makes sense. But how would i port this parameter:and these: CONST84 char **tablePtr = ? CONST84 char ***argvPtr = ?
Dec 22 2013
On Sunday, 22 December 2013 at 16:45:15 UTC, Benjamin Thaut wrote:Am 22.12.2013 17:02, schrieb Gary Willoughby:Ah right, so: struct Tcl_Obj * CONST * objv would be: const(Tcl_Obj*)* objv or const(Tcl_Obj*)[] objvOn Sunday, 22 December 2013 at 15:49:43 UTC, Gary Willoughby wrote: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 ThautThanks, that makes sense. But how would i port this parameter:and these: CONST84 char **tablePtr = ? CONST84 char ***argvPtr = ?
Dec 22 2013
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*)[] objvYes
Dec 22 2013
On Sunday, 22 December 2013 at 18:28:43 UTC, Benjamin Thaut wrote:Am 22.12.2013 18:39, schrieb Gary Willoughby:Great thanks! I thought i had a pretty good handle on C but porting some headers makes me scratch my head.Ah right, so: struct Tcl_Obj * CONST * objv would be: const(Tcl_Obj*)* objv or const(Tcl_Obj*)[] objvYes
Dec 22 2013
Am 22.12.2013 20:34, schrieb Gary Willoughby:On Sunday, 22 December 2013 at 18:28:43 UTC, Benjamin Thaut wrote: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 ThautAm 22.12.2013 18:39, schrieb Gary Willoughby:Great thanks! I thought i had a pretty good handle on C but porting some headers makes me scratch my head.Ah right, so: struct Tcl_Obj * CONST * objv would be: const(Tcl_Obj*)* objv or const(Tcl_Obj*)[] objvYes
Dec 22 2013
22.12.2013 11:06, Alexandr Druzhinin пишет:22.12.2013 07:47, Gary Willoughby пишет: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.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 22 2013