Compiler error with struct and const or #define

J

jon.harding

Hello, I am learning C so excuse me if this is a trivial question.

Here is some contrived code. It won't even compile with gcc. I need to
set a size of the int array at compile time. When I provide copies of
my code for evaluation, I want to set the dynamic size low. When I
provide copies of my code when someone buys it, I want to set the
dynamic size high (and recompile) and provide something more useful.
The line with baa1 is fine and when it is the only array in my struct,
it compiles fine. The lines baa2 and baa3 the compiler doesn't like.
Can someone advise of the syntax to dynamically size an array in a
struct at compile time with gcc/Ubuntu x86.
Thank you
Jon

#define TESTDYNAMICSIZE1 = 5
const testdynamicsize2 = 5;

struct foo1 {
int id;
int baa1[5];
int baa2[TESTDYNAMICSIZE1];
int baa3[testdynamicsize2];
};

int main(int argc, char *argv[]) {
return 0;
}
 
B

Ben Bacarisse

Hello, I am learning C so excuse me if this is a trivial question.

Here is some contrived code. It won't even compile with gcc. I need to
set a size of the int array at compile time. When I provide copies of
my code for evaluation, I want to set the dynamic size low. When I
provide copies of my code when someone buys it, I want to set the
dynamic size high (and recompile) and provide something more useful.
The line with baa1 is fine and when it is the only array in my struct,
it compiles fine. The lines baa2 and baa3 the compiler doesn't like.
Can someone advise of the syntax to dynamically size an array in a
struct at compile time with gcc/Ubuntu x86.

You've made only a very small mistake, but I'd alter your terminology.
It's not usual to refer to a compile-time size as "dynamic" even though
you may change it it from time to time. The phrase "dynamic size" is
more often used to describe thing (particularly arrays) whose
size changes during run-time.
#define TESTDYNAMICSIZE1 = 5

Drop the "=":

#define TESTDYNAMICSIZE1 5
const testdynamicsize2 = 5;

This one won't work. const objects in C are, rather confusingly, not
guaranteed to be compile-time constants.
struct foo1 {
int id;
int baa1[5];
int baa2[TESTDYNAMICSIZE1];
int baa3[testdynamicsize2];
};

<snip>
 
J

jon.harding

Hello, I am learning C so excuse me if this is a trivial question.
Here is some contrived code. It won't even compile with gcc. I need to
set a size of the int array at compile time. When I provide copies of
my code for evaluation, I want to set the dynamic size low. When I
provide copies of my code when someone buys it, I want to set the
dynamic size high (and recompile) and provide something more useful.
The line with baa1 is fine and when it is the only array in my struct,
it compiles fine. The lines baa2 and baa3 the compiler doesn't like.
Can someone advise of the syntax to dynamically size an array in a
struct at compile time with gcc/Ubuntu x86.

You've made only a very small mistake, but I'd alter your terminology.
It's not usual to refer to a compile-time size as "dynamic" even though
you may change it it from time to time.  The phrase "dynamic size" is
more often used to describe thing (particularly arrays) whose
size changes during run-time.
#define TESTDYNAMICSIZE1 = 5

Drop the "=":

#define TESTDYNAMICSIZE1 5
const testdynamicsize2 = 5;

This one won't work.  const objects in C are, rather confusingly, not
guaranteed to be compile-time constants.
struct foo1 {
              int id;
              int baa1[5];
              int baa2[TESTDYNAMICSIZE1];
              int baa3[testdynamicsize2];
};

<snip>

John and Ben, thank you. comp.lang.c is a very helpful and friendly
group.
Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top