function pointer and alias questions

BCS ao at pathlink.com
Tue Jul 29 13:35:16 PDT 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.




More information about the Digitalmars-d-learn mailing list