0
typedef struct
{
    char struct_variable_1a[10];
    char struct_variable_1b[12];
} struct1;

typedef struct
{
    char struct_variable_1a[10];
    char struct_variable_1b[12];
    int struct_variable_2c;
} struct2;

typedef struct1 *struct1_ptr;
typedef struct2 *struct2_ptr;



static void sampleFunction(struct1_ptr valueToInsert){
//this code does some stuff here
}


int main(){

struct1_ptr struct1_var = (struct1_ptr) malloc(sizeof(struct1));
strcpy(struct1_var->struct_variable_1a, "some value");
strcpy(struct1_var->struct_variable_1b, "some value");

sampleFunction(struct1_var);

return 0;
}

I have a sample code in C programming as shown above. In the main method, I am trying to pass a variable of type struct1_ptr for sampleFunction method call. This works like a charm. But when I want to pass a variable of type struct2_ptr, the compiler is throwing error. Basically, I am java developer. I want to reuse this sampleFunction method for any variable type in its parameter in general. Kindly help me in this.

7
  • 1
    Are struct_variable_1a and struct_variable_2a used the same way; are you trying to mimic inheritance? Commented Aug 27, 2013 at 13:49
  • not sure what you want, so let's approach this from a different angle that's faimiliar to you... how would you do this in java? Commented Aug 27, 2013 at 13:50
  • @user694733: yes exactly :). If not, atleast in a way that C can handle this stuff. Because If ever I have to call this sampleFunction method anywhere else in the code, I will be using the same implementation with different type of parameter. So I dont want to write a new method with the same implementation but with a different type of parameter. Commented Aug 27, 2013 at 13:52
  • @Arun: have you considered factoring common code into a helper function, and having two different front-end functions with different argument types? Alternately, you might consider a macro to generate the code, but that should be a last resort. Commented Aug 27, 2013 at 13:54
  • By the way, struct2 essentially includes struct1. Any reason why you can't just put a struct1 inside a struct2, and thus be able to pass either struct1 or struct2.struct1 to a function expecting a struct1? Commented Aug 27, 2013 at 13:55

3 Answers 3

3

You could emulate inheritance in C like this:

typedef struct
{
    char struct_variable_1a[10];
    char struct_variable_1b[12];
} struct1;

typedef struct
{
    struct1 parent; /* Must be first element! */
    int struct_variable_2c;
} struct2;

And then call like this:

sampleFunction((struct1_ptr)ptr_to_struct2);

or (I would favor this, because there's no cast):

sampleFunction(&ptr_to_struct2->parent);

Just note that you won't be able to access members of struct2 in function without casting it back.

These work for simple structures, but if you do anything more complex, using C++ would be more approriate.

Sign up to request clarification or add additional context in comments.

2 Comments

How will be the method signature for the sampleFunction method?
One you wrote would work: static void sampleFunction(struct1_ptr valueToInsert)
3

unfortunately C does not support method overloading / polymorphism. So you have to write your own methodology.

one is, to give the type as a first parameter to every overloaded function. Inside the function you walk along with a simple switch-case to jump to the original function implementation (like struct1_sampleFunction() and struct2_sampleFunction()).

another is, to put an RECORDCORE struct at the beginning of every struct and fill the type into this RECORDCORE, so each function can dispatch according to this data.

in both cases you have to change the prototype from "struct1_ptr value" to "void *value"

Comments

2

change your function from:

static void sampleFunction(struct1_ptr valueToInsert){
//this code does some stuff here
}

to:

static void sampleFunction(void* valueToInsert){
//this code does some stuff here
}

If you want to identify the type of the structure add a second parameter to your function that contains the size of the struct.

//Edit:
Inside the function you have to cast your variable back to the structure you want to use. for example:

strcpy(((struct1_ptr)valueToInsert)->struct_variable_1a, "some value");

1 Comment

Awesome dude. It worked like a charm. Thanks for this. I have been spending most of this day for this stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.