473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Function pointers to printf

Hello,

Can we have a function pointer to printf or any function with variable arguments?
I have tried in Microsoft Visual Studio C++ compiler
but could not compile.

Is this a compiler dependent problem.
If it can't be compiled what are the reasons for it

typedef int (*printf_ptr) (char *str, ...);

int my_printf (char *str, ...)
{
/* and the standard code for accessing var. args using
* va_args, va_start , va_end */
}

int main ()
{
int i = 10;
printf_ptr = printf;

printf_ptr (" i valus is %d",i);

printf_ptr = my_printf;

printf_ptr (" i valus is %d",i);
}
Thanks
Prakru
Nov 14 '05 #1
4 9555
Prakru wrote in news:cd**************************@posting.google.c om in
comp.lang.c++:
Hello,

Can we have a function pointer to printf or any function with variable
arguments?
I have tried in Microsoft Visual Studio C++ compiler
but could not compile.

Is this a compiler dependent problem.
If it can't be compiled what are the reasons for it

typedef int (*printf_ptr) (char *str, ...);
The Standard conforming declaration for std::printf is

int printf( char const *, ... ); /* In namespace std of course */

So your typedef needs to be:

typedef int (*printf_ptr) (char const *str, ...);

int my_printf (char *str, ...)
int my_printf( char const *str, ... )
{
/* and the standard code for accessing var. args using
* va_args, va_start , va_end */
}

int main ()
{
int i = 10;
printf_ptr = printf;


printf_ptr is a *type*, you need to create in instance of this type, do:

printf_ptr p = std::printf

[snip]
#include <cstdio>

typedef int (*printf_ptr) (char const *str, ...);

int main ()
{
int i = 10;
printf_ptr p = std::printf;

p( "i value is %d\n", i );
}

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Nov 14 '05 #2
Prakru wrote to both comp.lang.c and comp.lang.c++:

[My response is in the context of C. Since C++ people may have a
different view, I have set follow-ups to comp.lang.c only. Very few
questions actually belong in both newsgroups, since these are different
languages.]
Can we have a function pointer to printf or any function with variable arguments?
Yes.
I have tried in Microsoft Visual Studio C++ compiler
but could not compile.
Because you're not doing what you think you are.
Is this a compiler dependent problem.
No. Your errors are errors for all C compilers.
If it can't be compiled what are the reasons for it
See below your code ...
typedef int (*printf_ptr) (char *str, ...);

int my_printf (char *str, ...)
{
/* and the standard code for accessing var. args using
* va_args, va_start , va_end */
}

int main ()
{
int i = 10;
printf_ptr = printf;
printf_ptr (" i valus is %d",i);
printf_ptr = my_printf;
printf_ptr (" i valus is %d",i);
}


You are doing several things wrong. You are using a type (printf_ptr)
as if it were a variable (never declared, either). You are using a
signature for your my_printf and printf_ptr incompatible with that for
printf. Compare your code to the following:

#include <stdio.h>

typedef int (*printf_ptr) (const char *str, ...);

int my_printf(const char *str, ...)
{
(void *) str;
return 0;
}

int main()
{
int i = 10;
printf_ptr function;
function = printf;
function(" i valus is %d", i);
function = my_printf;
function(" i valus is %d", i);
return 0;
}
Nov 14 '05 #3
Rob Williscroft wrote to both comp.lang.c and comp.lang.c++:
Prakru wrote in news:cd**************************@posting.google.c om in
comp.lang.c++:
And comp.lang.c as well.
Since you don't know where Prakru is reading this, it is reasonable to
respond in both newsgroups but ...
The Standard conforming declaration for std::printf is


And similar things suggest that you should set follow-ups to
comp.lang.c++ only, as I have done with this post.
Nov 14 '05 #4
pr********@yahoo.com (Prakru) wrote in message news:<cd**************************@posting.google. com>...
Hello,

Can we have a function pointer to printf or any function
with variable arguments?
Yes. Make sure the complete argument list matches the
declaration of the printf functions.
typedef int (*printf_ptr) (char *str, ...);
printf doesn't try to modify its first argument. Therefore,
the first argument is actually const char*. You can't
assign &printf to printf_ptr.
int my_printf (char *str, ...)
{
/* and the standard code for accessing var. args using
* va_args, va_start , va_end */
}


Are you going to modify str? Then why do you want write access?

BTW, next time, read the error message. If you don't undertand it,
please post it as well so we can explain the error message.

Regards,
Michiel Salters.
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Albert | last post by:
Hi, I need to pass a pointer-to-member-function as a parameter to a function which takes pointer-to-function as an argument. Is there any way to do it besides overloading the function? Here...
11
by: Rajesh | last post by:
Dear All, Please let me know the advantage of function pointer? Is it fast calling function using function pointer? Is it possible to use function pointer to optimise code? Thanks and regards...
4
by: Isaac | last post by:
Hi mates I want to know a simple program of return array from function ? Do I need to use pointer to return the address of the first element in an array. Isaac
8
by: chook | last post by:
How i can call my function with some parameters, if i have a pointer to this function
27
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints...
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
54
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
10
by: Pedro Pinto | last post by:
Hi there! I'm creating a function that copies some information to a buffer and enters a delimiter string "//" between results. My issue here is when i print the buffer it appears this weird...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
0
by: anu29dolly | last post by:
hello i want to pass two pointers to function ... my code requirement is that i need to pass value tusb_dev1 and tusb_dev2 as it contains different data depending on the usb device found... and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.