N
Noob
Hello,
I have code that looks like this at the moment:
extern int do_it(char *name, char *thing, void *param);
struct s1;
struct s2;
int err;
if (some_condition)
{
struct s1 foo1 = { a, b, c };
err = do_it(name, "XYZ", &foo1);
}
else
{
struct s2 foo2 = { x, y, z, t, q, r, s };
err = do_it(name, "QSDF", &foo2);
}
if (err) do_something(err);
I don't have the source code for do_it (it's a library function).
I know that it looks at "thing" to decide how to use "param".
(Probably some form of strcmp).
I'd like to move the call to do_it after the if/else blocks,
to avoid duplicating code.
I know I can't write
char *thing;
void *param;
if (some_condition)
{
struct s1 foo1 = { a, b, c };
thing = "XYZ";
param = &foo1;
}
else
{
struct s2 foo2 = { x, y, z, t, q, r, s };
thing = "QSDF";
param = &foo2;
}
err = do_it(name, thing, param);
if (err) do_something(err);
because foo1 and foo2 don't "exist" outside their respective blocks.
(i.e. referring to them after they come out of scope has UB.)
I could init foo1 and foo2 BEFORE the test.
char *thing;
void *param;
struct s1 foo1 = { a, b, c };
struct s2 foo2 = { x, y, z, t, q, r, s };
if (some_condition)
{
thing = "XYZ";
param = &foo1;
}
else
{
thing = "QSDF";
param = &foo2;
}
err = do_it(name, thing, param);
if (err) do_something(err);
but one of the init would be just wasting cycles.
I'd want to be able to write something like
char *thing;
void *param;
struct s1 foo1;
struct s1 foo2;
if (some_condition)
{
foo1 = { a, b, c };
thing = "XYZ";
param = &foo1;
}
else
{
foo2 = { x, y, z, t, q, r, s };
thing = "QSDF";
param = &foo2;
}
err = do_it(name, thing, param);
if (err) do_something(err);
but it's not possible, is it?
Is there a nice solution?
Regards.
I have code that looks like this at the moment:
extern int do_it(char *name, char *thing, void *param);
struct s1;
struct s2;
int err;
if (some_condition)
{
struct s1 foo1 = { a, b, c };
err = do_it(name, "XYZ", &foo1);
}
else
{
struct s2 foo2 = { x, y, z, t, q, r, s };
err = do_it(name, "QSDF", &foo2);
}
if (err) do_something(err);
I don't have the source code for do_it (it's a library function).
I know that it looks at "thing" to decide how to use "param".
(Probably some form of strcmp).
I'd like to move the call to do_it after the if/else blocks,
to avoid duplicating code.
I know I can't write
char *thing;
void *param;
if (some_condition)
{
struct s1 foo1 = { a, b, c };
thing = "XYZ";
param = &foo1;
}
else
{
struct s2 foo2 = { x, y, z, t, q, r, s };
thing = "QSDF";
param = &foo2;
}
err = do_it(name, thing, param);
if (err) do_something(err);
because foo1 and foo2 don't "exist" outside their respective blocks.
(i.e. referring to them after they come out of scope has UB.)
I could init foo1 and foo2 BEFORE the test.
char *thing;
void *param;
struct s1 foo1 = { a, b, c };
struct s2 foo2 = { x, y, z, t, q, r, s };
if (some_condition)
{
thing = "XYZ";
param = &foo1;
}
else
{
thing = "QSDF";
param = &foo2;
}
err = do_it(name, thing, param);
if (err) do_something(err);
but one of the init would be just wasting cycles.
I'd want to be able to write something like
char *thing;
void *param;
struct s1 foo1;
struct s1 foo2;
if (some_condition)
{
foo1 = { a, b, c };
thing = "XYZ";
param = &foo1;
}
else
{
foo2 = { x, y, z, t, q, r, s };
thing = "QSDF";
param = &foo2;
}
err = do_it(name, thing, param);
if (err) do_something(err);
but it's not possible, is it?
Is there a nice solution?
Regards.