Solved

Global initialization of pointer to a function

Posted on 2006-11-21
6
242 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

757 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

20 Experts available now in Live!

Get 1:1 Help Now