function in parameter with return

Good afternoon, I need help with returning value from function. How can I save the return value to local variable ? I have problem with access to return value.

1
2
3
4
char* test(unsigned short x) {
...
return "something";
}


main func
1
2
3
4
void testme(char*(*f)(unsigned short x))
unsigned short i = 0;
..
char* t =  (*f)(i);


I'll be grateful for any help. Thanks.
Last edited on
I'm not sure exactly what your problem is. You should post a complete runnable example instead of meaningless snippets.

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

const char* test(unsigned short x) {
  if (x % 2 == 0)
      return "even";
  else
      return "odd";
}

void testme(const char*(*f)(unsigned short)) {
  unsigned short i = 7;
  const char* t = f(i);
  std::cout << t << '\n';
}

int main() {
    testme(test);
}

I forgot to mention that it is written in C. And as the problem show me something like this: Exception thrown at 0x00F57B60 in project...: 0xC0000005: Access violation executing location 0x00F57B60.
On this line
char* t = f(i);
Please post sufficient code to reproduce the problem.
The snippets you posted are not sufficient.
Ok, this is the chain of func.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void v_men(char*(*f)(unsigned short x))
{
	short i = 0;
	while( 1 == 1) {
		pm((*f)(x));
		... nothing special ...
}

void pm(char*(*f)(unsigned short x)) {
	unsigned short i = 0;
	...
	char * t = (*f)(i);
	printf("%s", t);
}

char* test_value(unsigned short mm)
{
	switch (mm) {
	case 0:
		return "WssX01"; break;
	case 1:
		return "WssX02"; break;
	case 2:
		return "Wx"; break;
	default:
		return "Err"; break;
}

int main() {
...
	v_men(test_value);
...
return err_indx;
}


I hope I've not forgotten anything.
Last edited on
closed account (z05DSL3A)
Something like? …
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
typedef const char* (*MenuFunc)(unsigned short);

void pm (MenuFunc mf)
{
	unsigned short i = 0;
	const char* t = mf (i);
	printf ("%s", t);
}

void v_men (MenuFunc mf)
{
	unsigned short i = 0;
	while (i < 10)
	{
		pm (mf);
		printf ("\n");
		i++;
	}
}

const char* test_value (unsigned short mm)
{
	switch (mm)
	{
	case 0: return "WssX01";
	case 1: return "WssX02";
	}
}

int main()
{
	v_men (test_value);
}
Almost .. But when I call the pm, I want to move the x value too. In v_men I have mistake and there should be unsigned short x
closed account (z05DSL3A)
Just pass the value as another parameter?...
1
2
3
4
5
6
void pm (MenuFunc mf, unsigned short x)
{
	unsigned short i = 0;
	const char* t = mf (x);
	printf ("%s", t);
}
thank you very much, I found two mistakes and I corrected them thanks to you.
Topic archived. No new replies allowed.