musicmatze
musicmatze

Reputation: 4288

Can I get a pointer to the current function?

Is it possible to get a pointer to the current function? If it is, how can I do so?

Motivation: I have a function doing some logging, and would like to call

log(currentfunc, "blabla")

Which does some output for example.

Upvotes: 4

Views: 1558

Answers (3)

CAMOBAP
CAMOBAP

Reputation: 5657

You can use combination of __FILE__ and __LINE__. It will be work with Microsoft and GCC compilers.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258648

I'm not sure about the pointer to a function, but the predefined identifier __func__ returns the name of the function. Maybe that can help...

In fact, I'd replace your function log with a macro so you don't have to paste in the name every time, as such:

#define log(x) log(__func__,x)

Upvotes: 2

Armen Tsirunyan
Armen Tsirunyan

Reputation: 133112

You can obtain the name of the current function (but not a pointer to it) via the predefined identifier __func__ which is part of C99.

log(__func__, "blabla");

Here's the reference

Upvotes: 5

Related Questions