www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - function pointer and alias questions

reply Michael P. <baseball.mjp gmail.com> writes:
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):



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.
Jul 29 2008
next sibling parent BCS <ao pathlink.com> writes:
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):
 

 
 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.
Jul 29 2008
prev sibling parent reply "Chris R. Miller" <lordSaurontheGreat gmail.com> writes:
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 tuto= rial(best beginner one I've found): If you ever experience problems with your build system, I also make a=20 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 :^)

=20
 And I wanted to clear up some things about Function Pointers and Aliase=
s. (It's part 12 of the tutorial, please turn to that part)
=20
 1. So, alias is just like a way of defining a type write, and with this=
line:
=20
 alias void function(char[]) GreeterFunction;
=20
 It defines a type named GreeterFunction that is a function that returns=
nothing and takes a character array/string as an argument?
=20
 2. This:
 static GreeterFunction[] greeters =3D
       [&sayHelloTo, &tellOff, &GreetWithSlang];=20
=20
 creates an array of the type GreeterFunction and places references of t=
he functions in that tutorial example in the array.
 And the static keyword makes the array size fixed at compile time inste=
ad 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=20 meaning in D though. A static statement is evaluated at runtime, or=20 else it throws an error if it isn't possible to evaluate it at runtime.
Jul 29 2008
parent reply BCS <ao pathlink.com> writes:
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.)
Jul 29 2008
parent reply "Chris R. Miller" <lordSaurontheGreat gmail.com> writes:
BCS wrote:
 Reply to Chris,
=20
 A static statement is evaluated at
 runtime, or else it throws an error if it isn't possible to evaluate
 it at runtime.
=20 That would be compile time? (OTOH "static if" and "static assert" are=20 construct in there own right rather than statements modified with stati=
c.) Yes, my brain flipped the two. Thanks for catching that error!
Jul 29 2008
parent reply BCS <ao pathlink.com> writes:
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 :(
Jul 29 2008
parent Michael P. <baseball.mjp gmail.com> writes:
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!
Jul 29 2008