Thread overview
function pointer and alias questions
Jul 29, 2008
Michael P.
Jul 29, 2008
BCS
Jul 29, 2008
Chris R. Miller
Jul 29, 2008
BCS
Jul 29, 2008
Chris R. Miller
Jul 29, 2008
BCS
Jul 29, 2008
Michael P.
July 29, 2008
Okay, so I've just started learning D. Set up the DMD 1.033 compiler on my "dev" computer without internet(yeah, it's possible), just used a USB to get all the tutorials/files on the computer. So I was using this tutorial(best beginner one I've found):

http://compsci.ca/v3/viewtopic.php?t=9518#

And I wanted to clear up some things about Function Pointers and Aliases. (It's part 12 of the tutorial, please turn to that part)

1. So, alias is just like a way of defining a type write, and with this line:

alias void function(char[]) GreeterFunction;

It defines a type named GreeterFunction that is a function that returns nothing and takes a character array/string as an argument?

2. This:
static GreeterFunction[] greeters =
      [&sayHelloTo, &tellOff, &GreetWithSlang];

creates an array of the type GreeterFunction and places references of the functions in that tutorial example in the array.
And the static keyword makes the array size fixed at compile time instead of having it be a dynamic array?

3. Quick question about the main function and it's parameters:

void main(char[][] commandLineArguments)

Why do you have to access the 2nd element of the array to get to the command line arguments? (By commandLineArguments[1]) What is the first element used for?
And is there any reason to use int main() and return 0; instead of just having void main()?

4.We can now declare a pointer to a function.

Code:
void function(char[]) greeterFunction;


The reason why greeterFunction is a pointer, and not just a normal variable, is because a function is a pointer to a piece of code in memory, right?


5.static void function(char[])[] greeters =
   [&sayHelloTo, &tellOff, &GreetWithSlang];


If the author were to use that piece of code, even though it's messy, it would make a static array named "greeters" of pointers to functions, filled with references to the functions that are in the code, right?


BTW, really liking D. I was learning C++ before, and decided to try out D. I love it!

Thanks to anyone who can help with these!
--Michael P.
July 29, 2008
Reply to Michael P.,

> Okay, so I've just started learning D. Set up the DMD 1.033 compiler
> on my "dev" computer without internet(yeah, it's possible), 

Yah! I'm not alone! (I use a laptop on both the private net and the internet but without connection sharing)

> just used
> a USB to get all the tutorials/files on the computer. So I was using
> this tutorial(best beginner one I've found):
> 
> http://compsci.ca/v3/viewtopic.php?t=9518#
> 
> And I wanted to clear up some things about Function Pointers and
> Aliases. (It's part 12 of the tutorial, please turn to that part)
> 
> 1. So, alias is just like a way of defining a type write, and with
> this line:
> 
> alias void function(char[]) GreeterFunction;
> 
> It defines a type named GreeterFunction that is a function that
> returns nothing and takes a character array/string as an argument?
> 

almost, the type is a pointer to a function that ....

> 2. This:
> static GreeterFunction[] greeters =
> [&sayHelloTo, &tellOff, &GreetWithSlang];
> creates an array of the type GreeterFunction and places references of
> the functions in that tutorial example in the array.
> 
> And the static keyword makes the array size fixed at compile time
> instead of having it be a dynamic array?

no it make it a global variable rather than a stack or member variable (depending on context)

> 
> 3. Quick question about the main function and it's parameters:
> 
> void main(char[][] commandLineArguments)
> 
> Why do you have to access the 2nd element of the array to get to the
> command line arguments? (By commandLineArguments[1]) What is the first
> element used for?
> 

as with C/C++ (and most posix stuff) the first arg is the name of the exe file

> And is there any reason to use int main() and return 0; instead of
> just having void main()?
> 

if you want to return a status code the the calling process...

> 4.We can now declare a pointer to a function.
> 
> Code:
> void function(char[]) greeterFunction;
> The reason why greeterFunction is a pointer, and not just a normal
> variable, is because a function is a pointer to a piece of code in
> memory, right?
> 

pointers and pointers to functions /are/ normal variables after a fashion

> 5.static void function(char[])[] greeters =
> [&sayHelloTo, &tellOff, &GreetWithSlang];
> If the author were to use that piece of code, even though it's messy,
> it would make a static array named "greeters" of pointers to
> functions, filled with references to the functions that are in the
> code, right?
> 

Yes


Hope that helps, if not, ask again.


July 29, 2008
Michael P. wrote:
> Okay, so I've just started learning D. Set up the DMD 1.033 compiler on my "dev" computer without internet(yeah, it's possible), just used a USB to get all the tutorials/files on the computer. So I was using this tutorial(best beginner one I've found):

If you ever experience problems with your build system, I also make a read-to-go toolchain at my site: www.fsdev.net.  You can use that if you break something.  I break stuff all the time, so I find myself eating my own dogfood quite often :^)

> http://compsci.ca/v3/viewtopic.php?t=9518#
> 
> And I wanted to clear up some things about Function Pointers and Aliases. (It's part 12 of the tutorial, please turn to that part)
> 
> 1. So, alias is just like a way of defining a type write, and with this line:
> 
> alias void function(char[]) GreeterFunction;
> 
> It defines a type named GreeterFunction that is a function that returns nothing and takes a character array/string as an argument?
> 
> 2. This:
> static GreeterFunction[] greeters =
>       [&sayHelloTo, &tellOff, &GreetWithSlang];
> 
> creates an array of the type GreeterFunction and places references of the functions in that tutorial example in the array.
> And the static keyword makes the array size fixed at compile time instead of having it be a dynamic array?

Almost.  The const keyword would do that.  Static makes it so that there is only one instance of it at run time.  Static does have another dual meaning in D though.  A static statement is evaluated at runtime, or else it throws an error if it isn't possible to evaluate it at runtime.



July 29, 2008
Reply to Chris,

> A static statement is evaluated at
> runtime, or else it throws an error if it isn't possible to evaluate
> it at runtime.
>

That would be compile time? (OTOH "static if" and "static assert" are construct in there own right rather than statements modified with static.)


July 29, 2008
BCS wrote:
> Reply to Chris,
> 
>> A static statement is evaluated at
>> runtime, or else it throws an error if it isn't possible to evaluate
>> it at runtime.
>>
> 
> That would be compile time? (OTOH "static if" and "static assert" are construct in there own right rather than statements modified with static.)

Yes, my brain flipped the two.  Thanks for catching that error!



July 29, 2008
Reply to Chris,

> BCS wrote:
> 
>> Reply to Chris,
>> 
>>> A static statement is evaluated at
>>> runtime, or else it throws an error if it isn't possible to evaluate
>>> it at runtime.
>> That would be compile time? (OTOH "static if" and "static assert" are
>> construct in there own right rather than statements modified with
>> static.)
>> 
> Yes, my brain flipped the two.  Thanks for catching that error!
>

If I had a dollar for every time I did something like that, I'd quit my job and write programs for fun... Oh wait that would be the same as now but I wouldn't get paid :(


July 29, 2008
BCS Wrote:

> Reply to Chris,
> 
> > BCS wrote:
> > 
> >> Reply to Chris,
> >> 
> >>> A static statement is evaluated at
> >>> runtime, or else it throws an error if it isn't possible to evaluate
> >>> it at runtime.
> >> That would be compile time? (OTOH "static if" and "static assert" are construct in there own right rather than statements modified with static.)
> >> 
> > Yes, my brain flipped the two.  Thanks for catching that error!
> >
> 
> If I had a dollar for every time I did something like that, I'd quit my job and write programs for fun... Oh wait that would be the same as now but I wouldn't get paid :(
> 
> 

lol :P
Anyways, thanks for helping me with those parts of D, appreciate it!