0

So I am still pretty new to C programming. I have learned Python though, so I am familliar to some of the codes.

For instance when I create a function in python, I am able to make it general and usable for different classes.

I want to do something similar here. I have two structs which look practically the same. I want to use the same function for both structs, but ofcourse I cant send in the struct name as an argument into the function. What do I do instead?

For now dont worry about what the function does. Its the principle of being able to use two structs in the same function that counts for me. If this is a totally wrong perspective, then I am sorry but this was my first thought when coming upon this problem.

typedef struct{
   int number;
   struct node *next;
}struct_1;

struct node *head;

typedef struct{
   int number;
   struct node *next;
}struct_2;

void main()
{
   int number1 = 10;
   int number2 = 20;
   function(number1);
   function(number2);
}

void function(int x, struct) // Here is where I want to be able to use 2 different structs for the same function
{
   struct *curr, *head;
   curr=(node1*)malloc(sizeof(node1));
   printf("%d", curr->number);
}
12
  • able to use can you please clarify?" Commented Feb 11, 2015 at 11:55
  • struct_1 and struct_2 have identical members in your example. What do you want to achieve? Do you want a linked list for various types of payloads? Commented Feb 11, 2015 at 11:56
  • 2
    This is a totally wrong perspective. Do not try to apply Python dynamic typing to C. If you have two structures - even with the same fields - for C they are different and that's it. There are some tricks to circumvent this, but since you are new to C do not start with tricks. Commented Feb 11, 2015 at 11:56
  • 1
    @diaco Why you want two structures for the same thing in the first place? Commented Feb 11, 2015 at 11:59
  • 1
    You want to have two copies of a struct. Not two distinct data types. Commented Feb 11, 2015 at 12:03

4 Answers 4

2

You could have two instances of one structure.
The function can accept either instance and process it as needed.

typedef struct{
    int number;
    struct node *next;
}mystruct;
void function(int x, mystruct *eachstruct);//prototype
int main()
{
    int number1 = 10;
    int number2 = 20;
    //declare two instances of mystruct
    mystruct list_1 = { 0, NULL};
    mystruct list_2 = { 0, NULL};
    // call the function with one or the other instance of mystruct
    function(number1, &list_1);
    function(number2, &list_2);
}

void function(int x, mystruct *eachstruct)
{
    //do stuff in function
    eachstruct->number = x;
    if ( eachstruct->next == NULL)
    {
        //do more stuff
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

C does not use duck typing as Python does so you cannot pass one structure that looks like other, completely unrelated structure as if it was this other structure.

Comments

0

Unfortunately C cannot do what you want.

Your options are:

  1. Refactor the code to use the same struct type for all items.
  2. Pass the fields of interest in the structs directly to the functions
  3. Write code to marshal the similar structs to a common struct.
  4. Play fast and loose with the type system and arrange shared elements the same way in the two different structs and cast your pointers.

If you just want a linked list check out how code re-use is achieved in the linux kernel

Comments

0

Answer: No, you cannot do it directly. Welcome to static typing.

There is a way to achieve something similar by using our beloved void * and some castings but, believe me, it is not what you want to do. If you really want to do it ask directly for it. You have been warned.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.