Questions tagged [function-pointers]
A function pointer is a pointer to a function, which can be stored in a variable. It allows a run-time choice of which function to run.
4,391
questions
-1
votes
0
answers
31
views
Function Pointer From Static Method Qt6 C++
I'm trying to create a function pointer to a static method using C++ in Qt Creator. I'm using a version of Qt6. I'm getting an error when running my attempt.
Here is my attempt.
//return_type (*...
0
votes
0
answers
7
views
Storing a member function address address and assigning it to a Func pointer
#include <functional>
#include <iostream>
#include <memory>
class BaseClass {
public:
virtual bool memberFunction(int arg1, int arg2) {
std::cout << "...
1
vote
3
answers
45
views
Getting incompatible pointer type warning because the return of the function is not void* but a struct pointer
This warning seems to not be necessary.
In my code, I have a function pointer type
(void*)(*function_pointer)(void* data)
and some functions require function pointers of this type as a parameter, ...
1
vote
1
answer
66
views
How to implemented comparison/less operator for member method pointers?
From my reading, I understand that it is possible to compare some pointer types in C++ using either operator< or std::less. This is useful if, for example, a pointer is used as a key for a std::...
1
vote
1
answer
95
views
Call function through pointer with no arguments [duplicate]
I want to implement a generic function returning a default value, and then use it through a function pointer as a replacement for other functions with different prototypes.
For example:
int ...
4
votes
1
answer
187
views
void* ptr = &func; compiles with msvc without any diagnostic but both gcc and clang rejects it
I am learning C++ using the book "C++ Primer" by Stanley. In particular, the section about "pointer conversions" says:
a pointer to any nonconst type can be converted to void*
...
1
vote
1
answer
71
views
How to Make a Function Returns Pointer to Array of Function Pointers
on this follow program I've tried to make a function that returns a pointer to array of 4 function pointers but I don't know the way how it works and how can I call it and receive the return value ...
1
vote
1
answer
79
views
Why aren't concept constraints considered contextual type information?
Consider:
void f(int); // (1)
void f(int, int); // (2)
auto pf1 = static_cast< void (*)(int) >(f); // Ok, address of (1)
auto pf2 = static_cast< void (*)(int, int) >(f); // ...
0
votes
0
answers
20
views
I will write an eprom driver.This writing process will be based on address and size.Iwill write it in Clanguage
I have an eeprom that communicates via SPI. I will write a driver for this eeprom, but this writing process will not contain any ready-made functions. Writing and reading operations will start from a ...
0
votes
1
answer
49
views
Pointer to OpenBLAS subroutines in fortran
In my code, I want to set pointers to OpenBLAS subroutines, to compile the code either in single or double precisions.
To do so, I define two modules and I define function interfaces for single(sgemv) ...
-1
votes
2
answers
83
views
How to use void(*FUNC)(void)
I've got a RTOS API which looks like that :
ISRInstall(int ISRnum,void(*FUNC)(void)).
Yet my desired callback function is void task(void*) for I need data input to calculate.
The platform is a SOC ...
0
votes
1
answer
56
views
C++ function pointer to non static member function [duplicate]
I have 2 classes: MeshInput and VoxelGrid. VoxelGrid has the member sdf which is a function pointer. Now I want to assign a non static function of another class to sdf. I can't change the signature of ...
0
votes
1
answer
31
views
Passing function pointer from one struct to another without adding dependency
I have two structs Foo and Bar, i want to store function callback of one struct (Foo) in the other (Bar), so that they don't know about each other. Is it possible in rust without Bar knowing that Foo ...
0
votes
2
answers
81
views
Function pointer not compiling cleanly
I am trying to familiarize myself with pointers to functions in C. I have looked online and found several examples of these. However, when I compile my program (using GCC [version (Debian 12.2.0-14) ...
3
votes
2
answers
187
views
Is it possible to have a list of pointers to member functions of one class as a member in another class where these functions will be called?
Let's say we have a class A, which has several setter functions with the identical function signature, like the following code:
class A
{
public:
void setter1(int whatever)
{
// set ...
3
votes
2
answers
105
views
How can I declare an array of pointers with blocks of NULL elems
I'm using an array of functions pointers, and I'd like to use it directly with their associated event IDs.
Problem is, event IDs start from 0x10 to 0x1C, and from 0x90 to 0xA5.
I don't want to write ...
-1
votes
1
answer
44
views
How can I fix the argument type mismatch between my key-callback function and glfwSetKeyCallback?
I have created a Camera class containing a keymap of std::map<int, void(Camera::*)() to facilitate an in-game option for key bindings and higher performance key-action lookup than what could be ...
1
vote
0
answers
49
views
Construct a callable argument in embedded Python with Python C API
I am writing code that starts in Python, then goes to C via ctypes and inside C it uses Python embedding to invoke a Python function, that is, the flow looks like this:
Python user code passes ...
-3
votes
1
answer
90
views
Storing a pointer to a function with any return type and any number or arguments
I'm writing a custom JIT (interpreted) programming language called Trunk, and I'm trying to parse functions from code and store them in a global name -> function map, for now. But I'm stuck ...
2
votes
2
answers
61
views
passing member function const and non-const overload to std::function
I was trying to get "more fluent" with function pointer manipulation and I've got an issue with passing a member function to std::function when there are two overload: const and non-const. ...
0
votes
0
answers
53
views
How can a foor loop with an array index be 3x slower then a switch case into a function that does the same thing?
I was benchmarking function pointers vs switch case statements in quick-bench and got the expected result. Function pointers are slower than switch case. But I wanted to remove the operation from the ...
1
vote
2
answers
53
views
Pointer variables generated in functions cannot be passed in parameters of function pointers
Topic:
Given a positive integer with no more than 5 digits, require:
Find out how many digits it is;
Output each digit separately;
Output each digit in reverse order. For example, if the original ...
0
votes
1
answer
77
views
Golang generics - cannot use a type in place of the same type while passing a function pointer
I am new to golang and I am working on the following struct:
type Flag[T any] struct {
defaultValue interface{}
}
Where T can be int or bool
I have defined the following function:
func (f Flag[T])...
2
votes
0
answers
49
views
How to call shared object function (loaded dynamically) from pointer in Go?
I try to call few shared object functions from Go. I don't want to write a C commented code to build a CGO interface for all functions.
I write my shared object like that:
#include <stdio.h>
...
3
votes
1
answer
90
views
How to represent a function/method type in a UML class diagram
Is there a standard way to represent a named function type in a UML class diagram?
By named function type, I mean a function or method type with a certain signature (parameters, return type, possibly ...
1
vote
1
answer
87
views
Calling a member function pointer without using this
I've written the following class that looks up a keyword and executes the associated function.
#include <iostream>
#include <string>
#include <map>
class Example;
typedef std::map&...
0
votes
3
answers
71
views
Function Pointers and Argurments
I have been trying to understand a post from geeksforgeeks and am not able to understand a code snippet. Here is the following post:
https://www.geeksforgeeks.org/returning-a-function-pointer-from-a-...
0
votes
1
answer
32
views
Function pointer declaration misreads argument type [closed]
I have problems using a struct as an argument in the declaration of a function pointer (MyFunction) below:
#include <iostream>
struct Record {
int x, y;
};
// MyFunction returns a changed ...
0
votes
1
answer
66
views
Calling Function Pointer from argument in a class member that points to another class member
I have an error in CLASS_A::Determine() and I don't know what mistake I have made:
expression preceding parentheses of apparent call must have (pointer-to-) function type
class CLASS_A
{
private:
...
-1
votes
1
answer
61
views
Function invocation in `std::bind`
TLDR: How does std::bind() actually work when calling a member function with an instance of a class or a this pointer of the class?
Notes:
I know what a function adapter and a function object are.
I ...
8
votes
1
answer
234
views
gcc and clang emit seemingly redundant function call
typedef struct foo
{
void (*const t)(struct foo *f);
} foo;
void t(struct foo *f)
{
}
void (*const myt)(struct foo *f) = t;
foo f = {.t = t};
int main(void)
{
f.t(&f);
myt(&...
-3
votes
3
answers
147
views
What is the downside of replacing if statements with function pointers?
I am looking for optimizing my update loops, and got around to this code:
// Define two lambda functions
auto iftrue = [](int x, int y) -> int {
return x;
};
auto iffalse = [](int x, int y) -&...
1
vote
1
answer
93
views
Setting a "function pointer"-like member to a noexcept lambda
I cannot achieve the effect in title, as implemented in the following snippet:
#include <functional>
#include <iostream>
#define USEFUNPTR
#define USESTDFUN
class Dummy {
public:
#...
0
votes
0
answers
36
views
Call a function with the address extracted from vector of lists, lists are address of functions
#include <list>
#include <vector>
#include <mutex>
typedef std::list<void *> funcList;
typedef struct {
funcList* functionsList;
char* listName;
}ListAccess_t;
namespace ...
0
votes
0
answers
43
views
How do I call a function using the elements of an array as attributes
I'm new to rust and found myself tinkering with some macros. I decided, that I want to try making a macro, that can take an array and call a function, using the elements of that array as arguments for ...
0
votes
2
answers
30
views
pass non-static member function with neither virtual inheritance nor templates [duplicate]
How do I pass the non-static member function eval of object Problem1 obj to the object Solver solver ?
#include<iostream>
#include<functional>
// non-templated class
struct Solver{
...
0
votes
2
answers
101
views
pointer to member function in C++ [duplicate]
I have problem in this line std::cout << &X::a << std::endl; this line print 1 supposed to printed address of &X::a
this my code
#include <iostream>
#include <string>...
1
vote
2
answers
73
views
What different between "typeSpecifier functionPointer = & definedFunction" and "typeSpecifier functionPointer = definedFunction"?
Please considering the following example first (this is a part of the code from Cplusplus.com):
#include <iostream> // std::cout
#include <functional> // std::function, std::negate
...
0
votes
1
answer
103
views
How can I cause indirect (function pointer) call to be correctly jump/branch predicted?
let's say I have a function that accepts a callback argument (example given in rust and C)
void foo(void (*bar)(int)) {
// lots of computation
bar(3);
}
fn foo(bar: fn(u32)) {
// lots of ...
0
votes
2
answers
98
views
Interesting problem about function pointer casting
I have made a interesting test about function pointer cast:
typedef bool (*BFUNC)(int n);
typedef int (*IFUNC)(int n);
typedef double (*DFUNC)(int n);
void vfunc(int n)
{
n++;
}
int _tmain(int ...
-1
votes
1
answer
46
views
Compile error for function pointer to member function in C++
I am getting compile error C2064 for this code.
class MainClass;
class MyClass
{
public:
MyClass();
void (MainClass::*functionPointer)();
void callback();
};
class MainClass
{
public:
...
0
votes
2
answers
62
views
How to define a function pointer for a function returning a 2-dimensional array?
I have written this program that defines a function to return a 1-dimensional array and a typedef declaration of a function pointer for the same. An instance of the function pointer is used to invoke ...
3
votes
4
answers
146
views
Why can you use a function as an if-statement condition without calling it? [duplicate]
I was wondering why both versions of this code have no error:
#include <iostream>
bool check_number(int n) {
return n < 5;
}
int main() {
int number = 6;
// (1) prints "...
-1
votes
1
answer
147
views
Are typedef declarations for bare function types (ie: not function pointers) legal in C89/C90?
Let's consider the following code:
#include <stdio.h>
#include <string.h>
typedef int INTFUNC(char *, char *);
INTFUNC lencmp;
int main(void) {
printf("%d\n", lencmp("...
1
vote
2
answers
189
views
Is it really legal for K&R to write "PFI strcmp, numcmp;" where PFI is typedef'd as "int (*)(char *, char *)"?
In The C Programming Language (Kernighan and Ritchie, 2nd ed) on p147, the authors show a typedef declaration
typedef int (*PFI)(char *, char *);
(PFI stands for "pointer to function returning ...
1
vote
2
answers
73
views
Passing a set of pointers to member functions of a template class
I have here a minimized version of code below, that won't compile.
#include<iostream>
#include<vector>
#include<set>
template<int D>
class Points{
public:
std::vector<...
-1
votes
2
answers
96
views
sizeof(pointer_to_a_function) vs sizeof (function_name)
Here is the program as text:
#include <stdio.h>
int f(int x){
return x+1;
}
int main() {
int (*p)(int) = f;
printf( "%d\n", sizeof(p) );
printf( "%d\n", ...
2
votes
1
answer
359
views
Implementing the defer keyword in C
Golang has a useful language construct called defer which allows the user to postpone a function's execution until the surrounding function returns. This is useful for ensuring that resources are ...
0
votes
1
answer
79
views
How to avoid mentioning a function pointer's arguments inside the definition of a function that takes it as argument?
I'm writing this C code that involves passing around a lot of function pointers, and sometimes writing all the arguments that a function pointer takes when defining a function that takes it as an ...
5
votes
2
answers
142
views
Creating a variadic function that prints any kind of format of ("c", "f", "i", "s"), but doesn't work
I've created a struct, that groups the format character and a pointer to the function which prints according to the formatter.
typedef struct formatter
{
char spec;
void (*print)(va_list);
} fmt;
...