Is there a way to pass the structure type of the function c

I have code with several functions very similar to each other in order to search for an item in a list based on the contents of one field in the structure. The only difference between the functions is the type of structure in which the search takes place. If I could pass a type, I could remove all duplicate code.

I also noticed that these functions also have a mutex lock, so I think I can leave them alone ...

+3
source share
8 answers

If you make sure that the field is placed in the same place in each such structure, you can simply specify the pointer to receive in the field. This method is used in many low-level system libraries, for example. BSD sockets.

struct person {
  int index;
};

struct clown {
  int index;
  char *hat;
};

/* we're not going to define a firetruck here */
struct firetruck;


struct fireman {
  int index;
  struct firetruck *truck;
};

int getindexof(struct person *who)
{
  return who->index;
}

int main(int argc, char *argv[])
{
  struct fireman sam;
  /* somehow sam gets initialised */
  sam.index = 5;

  int index = getindexof((struct person *) &sam);
  printf("Sam index is %d\n", index);

  return 0;
}

, , .

[ . , . ]

+6

, , . * - .

void * .

, *, , , - .

+3

, Csort() bsearch() . . , , . , .

, , , , , bsearch(), " ", , , , ( - , , . heap_search() heap_insert() C. , - , "c heap search", , " ", !)

+1

ID, , , , , :

#include <stdio.h>

typedef struct
{
    int id;
    int junk1;
} Foo;

typedef struct
{
    int id;
    long junk2;
} Bar;

typedef union
{
    struct
    {
        int id;
    } common;

    Foo foo;
    Bar bar;
} U;

int matches(const U *candidate, int wanted)
{
    return candidate->common.id == wanted;
}

int main(void)
{
    Foo f = { 23, 0 };
    Bar b = { 42, 0 };

    U fu;
    U bu;

    fu.foo = f;
    bu.bar = b;

    puts(matches(&fu, 23) ? "true" : "false");
    puts(matches(&bu, 42) ? "true" : "false");

    return 0;
}

, , . offsetof , OP, - :

#include <stddef.h>
#include <stdio.h>

typedef struct
{
    int id;
    int junk1;
} Foo;

typedef struct
{
    int junk2;
    int id;
} Bar;

int matches(const void* candidate, size_t idOffset, int wanted)
{
    return *(int*)((const unsigned char*)candidate + idOffset) == wanted;
}

#define MATCHES(type, candidate, wanted) matches(candidate, offsetof(type, id), wanted)

int main(void)
{
    Foo f = { 23, 0 };
    Bar b = { 0, 42 };
    puts(MATCHES(Foo, &f, 23) ? "true" : "false");
    puts(MATCHES(Bar, &b, 42) ? "true" : "false");

    return 0;
}
+1

- . , , . , .

0

You can do this with a parameterized macro, but most encoding rules will frown.


#include 
#define getfield(s, name) ((s).name)

typedef struct{
  int x;
}Bob;

typedef struct{
  int y;
}Fred;

int main(int argc, char**argv){
    Bob b;
    b.x=6;

    Fred f;
    f.y=7;

    printf("%d, %d\n", getfield(b, x), getfield(f, y));
}
0
source

The short answer is no. However, you can create your own method for this, that is, provide a specification for creating such a structure. However, it is not needed at all and is not worth the effort; just follow the link. ( callFuncWithInputThenOutput(input, &struct.output);)

0
source

I'm a little rusty on c, but try using the void * pointer as the type of the variable in the function parameter. Then pass the address of the function structure, and then use it the way you do.

void foo(void* obj);

void main()
{
  struct bla obj;
  ...
  foo(&obj);
  ...
}

void foo(void* obj)
{
  printf(obj -> x, "%s")
}
-1
source

Source: https://habr.com/ru/post/1696907/


All Articles