Solved

Global initialization of pointer to a function

Posted on 2006-11-21
6
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Kent Olsen
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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:
Kent Olsen 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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…
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 opening and writing to files in the C programming language.

751 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