0

I have completely different structs with single common member char *desc and I want to print them:

#include <stdio.h>

struct foo {
    float foo;
    char *desc;
};

struct bar {
    int barint;
    char *desc;
};

int main(int argc, char *argv[]) {

    struct foo ff[] = {
        { 1.1, "desc foo 1"}, { 2.2, "desc foo 2"}, { }
    };

    struct bar bb[] = {
        {1, "desc bar 1"}, {2, "desc bar 2"}, {3, "desc bar 3"}, { }
    };

    const struct foo *f = ff;
    const struct bar *b = bb; 
    int i;

    for (i = 0; f[i].desc; i++)
        printf("f[%d].desc: '%s'\n", i, f[i].desc);

    for (i = 0; b[i].desc; i++)
        printf("b[%d].desc: '%s'\n", i, b[i].desc);

    return 0;
}
$ ./a.out
f[0].desc: 'desc foo 1'
f[1].desc: 'desc foo 2'
b[0].desc: 'desc bar 1'
b[1].desc: 'desc bar 2'
b[2].desc: 'desc bar 3'

I'd like to have single macro/function which can take array of any of these structs as a parameter (there can be more than these two), and print desc member.

EDIT: defining macro is easy:

#define PRINT_DESC(obj) do { \
    for (i = 0; obj[i].desc; i++) \
        printf("obj[%d].desc: '%s'\n", i, obj[i].desc); \
} while (0)

But I need something more generic. I'd like const struct bar *b or const struct foo *f (or any other struct) assign to another internal library struct member and then do printing in the library via this library struct member. That member could be void *, but how to specify what struct to cast that pointer to?

Concrete example (sorry, not MCVE): planned to use it in LTP.

There is struct tst_test in tst_test.h, where I'd like to add void * pointer to struct, which has the same size as int test_variants member. And then print *desc of particular member in loop in tst_run_tcases():

for (tst_variant = 0; tst_variant < test_variants; tst_variant++) {
    if (tst_test->test_variants_desc)
        // TODO: print .desc for tst_test->test_variants_desc[tst_variant]

struct tst_test is defined in tests, e.g. futex_wait02, where struct futex_test_variants would be passed, because it has char *desc member (defined in futextest.h).

Other tests have different structs, that's why it cannot be pointer to struct futex_test_variants.

pevik
  • 4,523
  • 3
  • 33
  • 44
  • 1
    What have you tried? What problems do you have with your attmept? – Some programmer dude Mar 11 '21 at 08:18
  • If `ff` and `bb` have the same number of elements, then why not loop once and use the common index to output values from both structs? – David C. Rankin Mar 11 '21 at 08:23
  • Take these struct as completely different, only with `char *desc` in common. I haven't tried anything, I just have no clue. – pevik Mar 11 '21 at 08:35
  • Updated the question to more illustrate what I need to solve. – pevik Mar 11 '21 at 10:05
  • You might want to have a look at `_Generic` but tbh, it sounds like you simply picked the wrong language :) – klutt Mar 11 '21 at 10:22
  • 1
    But I don't think your question is very clear. Can you show exactly how you would like to call this function or macro? – klutt Mar 11 '21 at 10:31
  • 1
    Don't put it in comments. Edit the question and make sure it's self contained. And show a struct that doesn't work with the macro. – klutt Mar 11 '21 at 10:45
  • You do not buy anything working with `void` here, you would have to cast back to the appropriate type before you could dereference the pointer anyway, either with `*ptr` or `prt[x]`. – David C. Rankin Mar 12 '21 at 00:57

0 Answers0