Click here to Skip to main content
15,885,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to do point to function that return me a pointer
Like this function

C#
int *test(int x)
{
	int y=x*x;
	int *p=&x;
	return p;
	}


What I have tried:

I tried this option int main but i get error all the time

C#
int (**t)(int x);
		t=&test;


C#
int (**t)(int x);
		t=*test;


C#
int (**t)(int x);
    t=&(*test);

C#
int (**t)(int x);
		t=*(&test);
Posted
Updated 1-Dec-16 16:53pm

Try this
int* (*myfunc)(int x);

myfunc = test;

Call as
int* ret = myfunc(3);

It's been a while for me but give this a try.

Note: You're returning the address of an parameter variable. It's gone once you've returned to the calling routine. Big Error !!!
 
Share this answer
 
Comments
komokom 1-Dec-16 20:22pm    
You mean its call by referns?
If you can explain me how its work i will be glad
Advice: Take time to learn properly the language, here is a very god lecture to learn C and C++, answer included.
Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900