B
Bill Pursell
Suppose I have a structure with many members, and I pass a pointer to
that structure to a function. I'd like the prototype of the function
to specify that it will only be changing certain members of the
function, but I have only the following horrific way to do that. Can
someone please suggest a better alternative?
#include <stdio.h>
struct foo {
char *a;
char *b;
};
struct const_foo {
const char *a;
const char *b;
};
struct foo_const_a {
const char *a;
char *b;
};
struct foo_const_b {
char *a;
const char *b;
};
void modify_a(struct foo_const_b *f)
{
f->a = "a has been changed!";
}
void modify_b(struct foo_const_a *f)
{
f->b = "b has been changed!";
}
void print_foo(struct const_foo *f)
{
printf("f.a = %s\nf.b = %s\n", f->a, f->b);
}
int main(void)
{
struct foo F = {"a string", "b string"};
print_foo((struct const_foo*)&F);
modify_a((struct foo_const_b*)&F);
modify_b((struct foo_const_a*)&F);
print_foo((struct const_foo*)&F);
return 0;
}
that structure to a function. I'd like the prototype of the function
to specify that it will only be changing certain members of the
function, but I have only the following horrific way to do that. Can
someone please suggest a better alternative?
#include <stdio.h>
struct foo {
char *a;
char *b;
};
struct const_foo {
const char *a;
const char *b;
};
struct foo_const_a {
const char *a;
char *b;
};
struct foo_const_b {
char *a;
const char *b;
};
void modify_a(struct foo_const_b *f)
{
f->a = "a has been changed!";
}
void modify_b(struct foo_const_a *f)
{
f->b = "b has been changed!";
}
void print_foo(struct const_foo *f)
{
printf("f.a = %s\nf.b = %s\n", f->a, f->b);
}
int main(void)
{
struct foo F = {"a string", "b string"};
print_foo((struct const_foo*)&F);
modify_a((struct foo_const_b*)&F);
modify_b((struct foo_const_a*)&F);
print_foo((struct const_foo*)&F);
return 0;
}