Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Compiler error with struct and const or #define

Reply
Thread Tools

Compiler error with struct and const or #define

 
 
jon.harding@gmx.co.uk
Guest
Posts: n/a
 
      05-02-2012
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;
}


 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      05-02-2012
In <(E-Mail Removed)> http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> #define TESTDYNAMICSIZE1 = 5


You don't need an equal sign here.

> const testdynamicsize2 = 5;


> struct foo1 {
> int baa3[testdynamicsize2];
> };


This would work if it were inside a function, but it isn't allowed in
global scope.

--
John Gordon A is for Amy, who fell down the stairs
(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      05-03-2012
(E-Mail Removed) writes:

> 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>
--
Ben.
 
Reply With Quote
 
jon.harding@gmx.co.uk
Guest
Posts: n/a
 
      05-03-2012
On May 3, 1:00*am, Ben Bacarisse <(E-Mail Removed)> wrote:
> (E-Mail Removed) writes:
> > 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>
> --
> Ben.


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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
How to let C++ compiler raise a warning or error when a const is assigned to a const reference. PengYu.UT@gmail.com C++ 14 09-17-2006 11:08 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments