You are not logged in.

#1 2025-06-15 17:49:53

lo7777799
Member
Registered: 2023-07-25
Posts: 64

C++ problems with function-pointers ...

hello, here is my code ...

#include <iostream>

using namespace std ;

int funktion_1(int a){
    cout << "dies ist funktion1... " << endl ;
    return 0 ;
}

int funktion_2(int a){
    cout << "dies ist funktion2... " << endl ;
    return 0 ;
}

inline int funktion_3(int a){
    cout << "dies ist funktion3... " << endl ;
    return 0 ;
}

    int funktion_4(int a){
        cout << "dies ist funktion4... " << endl ;
        return 0 ;
    }

    inline int funktion_5(int a){
        cout << "dies ist funktion5... " << endl ;
        return 0 ;
    }


int main(){

    // Jetzt kommen "inline"-Funktionen ...
    int a ;

    int *main_inline(a) = &main(a) ;

    int *funktion1_not_inline() ;

    int *funktion2_not_inline() ;
   
    int *funktion3_inline() ;
   
   


    int *funktion4_not_inline() ;

    int *funktion5_inline() ;



    cout << "die main-funktion hat die adresse... " << main_inline << endl ;

    cout << "die funktion1, nicht inline, hat die adresse... " << funktion1_not_inline << endl ;

    cout << "die funktion2, nicht inline, hat die adresse... " << funktion2_not_inline << endl ;

    cout << "die funktion3, inline, hat die adresse... " << funktion3_inline << endl ;

    cout << "die funktion4, nicht inline, hat die adresse... " << funktion4_not_inline << endl ;

    cout << "die funktion5, inline, hat die adresse... " << funktion5_inline << endl ;

   



    return 0 ;
}

i want to build pointers to look at the function-adresses... i also want a pointer to main-function ... then i want to let read out the adresses... i also want to test the inline-command... 1 function is inline ...

what should i change to make that code runnable ?

and: can i do something similar with references(&) instead of pointers(*)?

Last edited by lo7777799 (2025-06-15 17:52:20)

Offline

#2 2025-06-15 18:46:55

airbus777neo
Member
Registered: 2025-06-03
Posts: 35

Re: C++ problems with function-pointers ...

Hi,

In order to declare a pointer to a function in C++ you would have to use the following syntax:

return_type (*pointer)(arg_types);
e.g. int (*funktion1_not_inline)(int);

So for example with your main_inline pointer

int (*main_inline)();

And then initialise it with

main_inline = main;

Which can therefore be combined into:

int (*main_inline)() {main};

This would then apply to all the other functions respectively

Lastly, to print the address

std::cout << &main_inline;

Last edited by airbus777neo (2025-06-16 19:05:44)


Just a computer fanatic finally beginning to etch out of his shell in experience
"The man who chases two rabbits catches none." — Confucius

Offline

#3 2025-06-15 18:55:43

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 367

Re: C++ problems with function-pointers ...

lo7777799 wrote:

hello, here is my code ...

This code is not valid C++ code.

lo7777799 wrote:
    int *funktion1_not_inline();

This declares a function which returns pointer to int. However, you already have defined function with the same name which returns int.

If you want to declare a pointer to a function which returns int, you can write

    int (*p)();  // uninitialized pointer
    int (*p1)() = funktion1_not_inline;  // initialized pointer
    // or
    int (*p2)() { funktion2_not_inline };  // initialized pointer
lo7777799 wrote:

i want to build pointers to look at the function-adresses...

See notes on std::basic_ostream<CharT,Traits>::operator<<

There are no overloads for pointers to non-static members, pointers to volatiles,(until C++23) or function pointers (other than the ones with signatures accepted by the (18-20) overloads).
    · Attempting to output such objects invokes implicit conversion to bool, and, for any non-null pointer value, the value 1 is printed (unless boolalpha was set, in which case true is printed).

lo7777799 wrote:

i also want a pointer to main-function ... then i want to let read out the adresses... i also want to test the inline-command... 1 function is inline ...

Why do you need this? What is your goal?

Offline

Board footer

Powered by FluxBB

OSZAR »