A
Ann O'Nymous
I think I know the answer to this is "no", but I'll ask anyway.
Is it possible to define a struct of arbitrary size as a parameter to a
routine, with its size passed in as another parameter or parameters?
By this, I mean something like this:
int foo(struct A *ptr; int size1, int offset, int size2) {
where "struct A" is something like this:
struct A {
char field1[offset];
char field2[size2];
char field3[size1-offset-size2];
};
That is, ptr is a pointer to a struct with a size of size1
(it's actually a pointer to an array of these things, so that
ptr++ advances to the next one), it has 3 fields, the first size
"offset", the second size "size2", the third containing the remainder.
In other words, if I do
foo(pointer,20,10,4);
it's as if foo had the lines in it:
struct A {
char field1[10];
char field2[4];
char field3[6]; // 20-10-4 = 6
};
and a ptr++ in foo would advance it by 20.
How about this:
foo(array,w,h) int w,h; char array[w][h]; {
int i,j;
for (i=0; i<w; i++ {
for (j=0; j<h; j++) {
array[j] = blablabla();
}
}
This isn't overly important, as I can do what I really need to do using
pointers to char. Just another "can I do this this way?" question.
Is it possible to define a struct of arbitrary size as a parameter to a
routine, with its size passed in as another parameter or parameters?
By this, I mean something like this:
int foo(struct A *ptr; int size1, int offset, int size2) {
where "struct A" is something like this:
struct A {
char field1[offset];
char field2[size2];
char field3[size1-offset-size2];
};
That is, ptr is a pointer to a struct with a size of size1
(it's actually a pointer to an array of these things, so that
ptr++ advances to the next one), it has 3 fields, the first size
"offset", the second size "size2", the third containing the remainder.
In other words, if I do
foo(pointer,20,10,4);
it's as if foo had the lines in it:
struct A {
char field1[10];
char field2[4];
char field3[6]; // 20-10-4 = 6
};
and a ptr++ in foo would advance it by 20.
How about this:
foo(array,w,h) int w,h; char array[w][h]; {
int i,j;
for (i=0; i<w; i++ {
for (j=0; j<h; j++) {
array[j] = blablabla();
}
}
This isn't overly important, as I can do what I really need to do using
pointers to char. Just another "can I do this this way?" question.