Solved

Global initialization of pointer to a function

Posted on 2006-11-21
6
249 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: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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert image to byte array 8 208
How to create frequencies of a variable from SAS dataset? 10 133
smtp c source code 7 85
Loading flat file data in tables 2 39
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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

808 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