1

I listed some example code below and the question is if there is a way for the function_name to access the value of number from struct_name?

typedef struct struct_name {  
int number  
void (*func)();  
} * struct_name_ptr;  

void function_name() {  
//access number from struct  
}

main() {  
  struct_name_ptr newobject;  
  newobject->func=&function_name;  
  newobject->func(); //can it print the value of the number in the structure above?  
} 
Andrew
  • 6,254
  • 16
  • 59
  • 93
  • 1
    It is not possible unless you change the signature of `func` to accept parameters. – UmNyobe May 27 '12 at 08:06
  • See this question [How can I simulate OO style polymorphism in C?](http://stackoverflow.com/questions/524033/how-can-i-simulate-oo-style-polymorphism-in-c) – Bo Persson May 27 '12 at 10:24
  • this cannot work; you have declared `struct_name_ptr` as a pointer, `newobject` points to undefined memory, a dereference is undefined behavior – user411313 May 27 '12 at 11:42

5 Answers5

4

Uh - no.

A struct can certainly contain a function pointer. But the function you call wouldn't have any knowledge of the struct. Unless you passed a pointer as a function argument, or made the struct global.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

With my limited knowledge of programming, I don't think this is possible. Though the struct contains a function pointer, the address of the function assigned to it is different and I don't think there will be anyway for it to access it unless you pass it as an argument.

Jay
  • 24,173
  • 25
  • 93
  • 141
1

Well, two things, struct_name->number should have a value, and it either needs to be in the same scope as &function_name or it needs to be explicitly passed. Two ways to do it:

/* Here is with a global calling struct */

#include<stdio.h>

typedef struct struct_name {
    int number;
    void (*func)();
    } * struct_name_ptr;

struct struct_name newobject = { 0 }; 

void function_name() {
    printf("%d",struct_name);
}

void main() {

    struct struct_name_ptr newobject;

    newobject->func=&function_name;
    newobject->func();
}

/* And one with a modified function_name */

#include<stdio.h>

typedef struct struct_name {
    int number;
    void (*func)();
    } * struct_name_ptr;


void function_name(struct_name) {
    printf("%d",struct_name);
}

void main() {

    struct struct_name_ptr newobject;
    newobject.number = 0;

    newobject->func=&function_name;
    newobject->func(newobject);
}
rhoyerboat
  • 204
  • 1
  • 5
1

No, a pizza won't ever know what the pizza delivery guy, who delivered it, looks like.

A regular function is just an address in memory. It can be called using a function pointer like in this case. In any case: The function won't know how it was called. In particular it won't know that it was called using a function pointer that's part of (a piece of memory corresponding to) some struct.

When using a language with classes like C++, member functions will have a hidden argument which is a pointer to the class instance. That's how member functions know about their data.

olovb
  • 2,164
  • 1
  • 17
  • 20
1

You can 'simulate' a simple OOP in plain C, for your example like:

typedef struct {
int number;
void (*func)();
} class;

void function_name(class *this) {
  printf("%d",this->number);
}

#define CALL(c,f) c.f(&c)

int main() {
  class object={12345,function_name};

  CALL(object,func); // voilá
}
user411313
  • 3,930
  • 19
  • 16