Functions and Asterixes, (*)

I've been learning C++ off of learncpp.com and its been great and very straight forward. There is one thing that I have been seeing around a lot, (on different sites as well as learncpp.com) that I do not understand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>


void *notmain(void)
{
	std::cout << "Swag" << std::endl;

	return 0;
}

int main()
{
	notmain();

	return 0;
}



For example, this is a simple program that I created to show what I am talking about. I'm not sure what the ' * ' does between the ' Void ' and my ' notmain ' function.

Thanks,

Zach
It means the function returns a void pointer, as opposed to just void (nothing).
Last edited on
What if it was

 
int *notmain()
Then it returns an int pointer. The * in a type indicates a pointer.
It returns the pointer address if I type this then, I'm guessing?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>


int *notmain()
{
	std::cout << "Swag" << std::endl;
	
	return 0;
}

int main()
{
	std::cout << notmain << std::endl;
	std::cout << *notmain << std::endl;
	return 0;
}
In:

1
2
	std::cout << notmain << std::endl;
	std::cout << *notmain << std::endl;


both notmain and *notmain are equivalent and are converted to bool. You can see this quite clearly by adding another line between line 12 and line 13:
std::cout << std::boolalpha ;

See:
http://stackoverflow.com/questions/2064692/how-to-print-function-pointers-with-cout
http://stackoverflow.com/questions/2795575/how-does-dereferencing-of-a-function-pointer-happen
Topic archived. No new replies allowed.