function pointer and alias questions

Michael P. baseball.mjp at gmail.com
Tue Jul 29 13:20:56 PDT 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.


More information about the Digitalmars-d-learn mailing list