Solved

Global initialization of pointer to a function

Posted on 2006-11-21
6
244 Views
Last Modified: 2010-04-15
Hi Experts,

Following is my code:

// --- header.h-----------------------------------

#ifndef HED_H_
#define HED_H_
#include <stdio.h>

int (*pf) ();
void qsort(int ( *fn)(int));
void test1();

#endif /*HED_H_*/
//----------------------

// -- main.c -------------------------------------
#include "header.h"

int f(int a){
      printf("%d\n",a);
      return a+10;
}

void main()
{
             qsort(f);      
}
// -----------------------------------------

// --- helper.c-----------------------------------

#include "heder.h"
void qsort(int( *cmpFunc)(int))
{
      pf = cmpFunc;
      i = pf(35);                                    //output: 35
      printf("%d\n",i);              //output: 45
}

void test1()
{
    int i = pf(45);                                             //<--- ERROR
    printf("%d\n",i);
}
// -------------------------------

Within a method function pointer is working fine, but how can I use the same function pointer in other functions without assigning it again and again.
More precisely if I say, how can I make it as global?
0
Comment
Question by:rrahulgupta
  • 3
  • 2
6 Comments
 
LVL 24

Expert Comment

by:fridom
ID: 17993683

To make it a bit simpler  write:

#include <stdio.h>


typedef int (*my_fun_ptr)(void);

int foo(void) {
  return 1;
}

static my_fun_ptr my_f_ptr = foo;


int main (void) {
  printf("result from my_f_ptr = %d\n", my_f_ptr());
  return 0;
}

That's all there is to do

Regards
Friedrich



0
 

Author Comment

by:rrahulgupta
ID: 17994926
Hi Friedrich, Thanks for your comment.
Actually, I have a requirement of that kind as I had written the files.
Actual function is implemnted anywhere else i.e. outside my code. I just have a fuction that will take poniter to function as parameter.
I want to use this fuction pointer in other functions also.
Did you get what I wanted?

Thank you.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 17995772
Hi rrahulgupta,

I see a couple of things at play here.  The first is in your declaration of *i* in test1.  Note that you declare the variable and initialize it to the return value of a function call on the same line.  That's asking a lot of a compiler/linker.  Make it two lines like you do in main().  That'll get you past the error

To initialize it only once but use it in several modules, declare the variable *pf* in main.c.  header.h and heder.h should prototype it, but not declare it.  Now you can initialize it in main() and call it from anywhere, including modules in other source objects.


Good Luck,
Kent
0
Free book by J.Peter Bruzzese, Microsoft MVP

Are you using Office 365? Trying to set up email signatures but you’re struggling with transport rules and connectors? Let renowned Microsoft MVP J.Peter Bruzzese show you how in this exclusive e-book on Office 365 email signatures. Better yet, it’s free!

 

Author Comment

by:rrahulgupta
ID: 17996167
Hi Kent,

>>The first is in your declaration of *i* in test1.  Note that you declare the variable and initialize it to the return value of a function call on the same line.
Thanks for the suggestion.

>>To initialize it only once but use it in several modules, declare the variable *pf* in main.c.  header.h and heder.h should prototype it, but not declare it.  Now you can initialize it in main() and call it from anywhere, including modules in other source objects.
I really didn't get this. I will appriciate you if you can explain that with some code snippet.
0
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 17996261

Sure, taking your original code, just these few changes:

// --- header.h-----------------------------------

#ifndef HED_H_
#define HED_H_
#include <stdio.h>

//  int (*pf) ();                // --  defined in main.c  --
void qsort(int ( *fn)(int));
void test1();

#endif /*HED_H_*/
//----------------------

// -- main.c -------------------------------------
#include "header.h"

int (*pf) ();                 // -- declare fp  --

int f(int a){
     printf("%d\n",a);
     return a+10;
}

void main()
{
             qsort(f);    
}
// -----------------------------------------

// --- helper.c-----------------------------------

#include "heder.h"

extern int (*pf) ();        // -- should probably be in heder.h --

void qsort(int( *cmpFunc)(int))
{
     int i;

     pf = cmpFunc;
     i = pf(35);                                    //output: 35
     printf("%d\n",i);            //output: 45
}

void test1()
{
    int i;

    i = pf(45);
    printf("%d\n",i);
}
// -------------------------------


Now pf is set globally and accessible from main.c, helper.c, and any other source module that you want to add.


Kent
0
 

Author Comment

by:rrahulgupta
ID: 17997929
Hi Kent,
Thanks for comments. The code you posted, is working fine. But my scenario is a bit different.
Actaully, there are two layers of design I have to follow:
1. header.h and helper.c falls in the one layer.
2. main.c in the other layer.

Now, the IInd layer has dependency on the Ist layer but vise-versa is not true.
And the apprach to told, I have to define function pointer in main.
Is there way to do this as I'm thinking?
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to setup iphone app subscription service 3 119
Directory does exist 19 144
First character of input string truncated with scanf 3 94
Arduino EDI - Programming language 3 80
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now