Pointer function to main

Feb 13, 2014 at 8:27pm
How do you call the pointer function back to main? I already try "&" didnt work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
For example: I have prototype 

void example (int, int, *int,*int,*int,*int);
int main
{
      ............
     example (int, int, &int,&int,&int,&int); // didnt work.
}
void example (int, int, *int,*int,*int,*int);
{

    putting code here!
}
Last edited on Feb 13, 2014 at 9:59pm
Feb 13, 2014 at 8:33pm
You wouldn't pass data types. You'd pass actual arguments.

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

void exampleFunction( int foo, int *p_foo );

int main( int argc, const char *argv[] )
{
    int a = 0;

    exampleFunction( a, &a );

    return 0;   
}

void exampleFunction( int foo, int *p_foo )
{
    std::cout << "Function called\n";
}
Feb 13, 2014 at 8:52pm
as i say it didnt work
Feb 13, 2014 at 9:21pm
What do you mean by call pointer function back to main? When function execution ends, it goes back to the place it got called, and in this case - it would be main function.

PS. "Pointer function"(which I would define, basing on your words, as a function that takes some pointer types as arguments) is not different from "normal" function.
Last edited on Feb 13, 2014 at 9:23pm
Feb 13, 2014 at 9:52pm
because it returns an int, you haveto store it in a variable.
int ex = example(//parameters....);
Feb 13, 2014 at 9:59pm
in pointer function I have it to find a smallest number now I need to get that information back to main so i can cout in main not in the function
Feb 13, 2014 at 10:00pm
sorry it a void function!
Feb 13, 2014 at 10:02pm
Then you need to pass by reference.

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

void exampleFunction( int &foo );

int main( int argc, const char *argv[] )
{
    int a = 0;

    exampleFunction( a );

    std::cout << "a is now: " << a << std::endl;

    return 0;   
}

void exampleFunction( int &foo )
{
    foo = 10;
}
Topic archived. No new replies allowed.