简体   繁体   中英

C - Functions Structs

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 struct s, 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 struct s 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);
}

C不像Python那样使用鸭子类型,因此您不能传递看起来像其他完全无关的结构的结构,就好像它是另一结构一样。

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
    }
}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM