Solved

Global initialization of pointer to a function

Posted on 2006-11-21
6
247 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

809 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