455,434 Members | 1,878 Online
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

# Hello i am new to this C programs

 P: n/a const struct { float a0; float a1; float a2; }A_coeffs[SECTIONS]; i am confused to declare the value for this structure correct me thanq i have declared this there is compiler error A_coeffs[]= { {1.254285,2.508570,1.254285}, {1.254285,2.508570,1.254285}, {1.254285,2.508570,1.254285}, }; Nov 15 '05 #1
11 Replies

 P: n/a This will assign value as follows a_coeffs[0].a0=1.254285, a_coeffs[0].a1=2.508570, a_coeffs[0].a2=1.254285, a_coeffs[1].a0=1.254285, a_coeffs[1].a1=1.508570, a_coeffs[1].a2=1.254285, like this it will go on Nov 15 '05 #2

 P: n/a In article <11**********************@g44g2000cwa.googlegroups .com>, siva writesconst struct{ float a0; float a1; float a2;}A_coeffs[SECTIONS];A_coeffs[]={ {1.254285,2.508570,1.254285}, {1.254285,2.508570,1.254285}, {1.254285,2.508570,1.254285},}; Nearly. #define SECTIONS 3 const struct { float a0; float a1; float a2; }A_coeffs[SECTIONS]= { {1.254285,2.508570,1.254285}, {1.254285,2.508570,1.254285}, {1.254285,2.508570,1.254285}, }; Nov 15 '05 #3

 P: n/a thanq u have give me good solution Nov 15 '05 #4

 P: n/a i am confused to declare the value for this structure correct me thanq i have declared this there is compiler error A_coeffs[]= }; Please quote the compiler error. It helps in debugging. Regards, Frodo Nov 15 '05 #5

 P: n/a siva wrote: const struct { float a0; float a1; float a2; }A_coeffs[SECTIONS]; Here you have declared it as being const, which means you are not allowed to write to it. i am confused to declare the value for this structure correct me thanq i have declared this there is compiler error A_coeffs[]= { {1.254285,2.508570,1.254285}, {1.254285,2.508570,1.254285}, {1.254285,2.508570,1.254285}, }; Here you try to write to it (which is not allowed) and you are also trying to write the hole array, which is not allowed anyway. Assuming these really are constant coefficients you want something like: const struct { float a0; float a1; float a2; } A_coeffs[] = { {1.254285,2.508570,1.254285}, {1.254285,2.508570,1.254285}, {1.254285,2.508570,1.254285}, }; #define SECTIONS ((sizeof A_coeffs) / (sizeof A_coeffs[0])) and get rid of your current definition of SECTIONS. This is pretty basic stuff so you probably need to reread the sections your text book has on arrays, initialisers, const and sizeof. -- Flash Gordon Living in interesting times. Although my email address says spam, it is real and I read it. Nov 15 '05 #6

 P: n/a In article <68************@news.flash-gordon.me.uk>, Flash Gordon writes#define SECTIONS ((sizeof A_coeffs) / (sizeof A_coeffs[0]))and get rid of your current definition of SECTIONS. Not necessarily. In many cases #define'ing SECTIONS in advance of the declaration of the array would be preferable because it might be calculated based upon something else, sourced from another file, or simply more obvious to the developer that way, and the point of then using it in the declaration of the array is to stop the programmer forgetting to then initialise all the elements. Nov 15 '05 #7

 P: n/a Stephen wrote: In article <68************@news.flash-gordon.me.uk>, Flash Gordon writes#define SECTIONS ((sizeof A_coeffs) / (sizeof A_coeffs[0]))and get rid of your current definition of SECTIONS. Not necessarily. In many cases #define'ing SECTIONS in advance of the declaration of the array would be preferable because it might be calculated based upon something else, sourced from another file, or simply more obvious to the developer that way, and the point of then using it in the declaration of the array is to stop the programmer forgetting to then initialise all the elements. It does not necessarily stop the programmer from forgetting to initialise all the elements, because there is not requirement for the compiler to complain at you for forgetting to initialise all of the elements. I'm not saying that there is never a good reason to define the size in some other way, but for a const array (which it was in this case) I believe that what I suggested is generally better. -- Flash Gordon Living in interesting times. Although my email address says spam, it is real and I read it. Nov 15 '05 #8

 P: n/a "Flash Gordon" wrote in message news:qv************@news.flash-gordon.me.uk... Stephen wrote: In article <68************@news.flash-gordon.me.uk>, Flash Gordon writes#define SECTIONS ((sizeof A_coeffs) / (sizeof A_coeffs[0]))and get rid of your current definition of SECTIONS. Not necessarily. In many cases #define'ing SECTIONS in advance of the declaration of the array would be preferable because it might be calculated based upon something else, sourced from another file, or simply more obvious to the developer that way, and the point of then using it in the declaration of the array is to stop the programmer forgetting to then initialise all the elements. It does not necessarily stop the programmer from forgetting to initialise all the elements, because there is not requirement for the compiler to complain at you for forgetting to initialise all of the elements. With an array, if you initialize at least one element, they all get initialized (but perhaps not to the desired values :-)) -Mike Nov 15 '05 #9

 P: n/a Mike Wahler wrote: "Flash Gordon" wrote in message news:qv************@news.flash-gordon.me.uk...Stephen wrote:In article <68************@news.flash-gordon.me.uk>, Flash Gordon writes #define SECTIONS ((sizeof A_coeffs) / (sizeof A_coeffs[0]))and get rid of your current definition of SECTIONS.Not necessarily.In many cases #define'ing SECTIONS in advance of the declaration of thearray would be preferable because it might be calculated based uponsomething else, sourced from another file, or simply more obvious to thedeveloper that way, and the point of then using it in the declaration ofthe array is to stop the programmer forgetting to then initialise allthe elements.It does not necessarily stop the programmer from forgetting to initialiseall the elements, because there is not requirement for the compiler tocomplain at you for forgetting to initialise all of the elements. With an array, if you initialize at least one element, they all get initialized (but perhaps not to the desired values :-)) This, indeed, was what I meant :-) -- Flash Gordon Living in interesting times. Although my email address says spam, it is real and I read it. Nov 15 '05 #10

 P: n/a In article , Flash Gordon writesMike Wahler wrote: "Flash Gordon" wrote in message news:qv************@news.flash-gordon.me.uk...Stephen wrote:In article <68************@news.flash-gordon.me.uk>, Flash Gordon writes >#define SECTIONS ((sizeof A_coeffs) / (sizeof A_coeffs[0]))>>and get rid of your current definition of SECTIONS.Not necessarily.In many cases #define'ing SECTIONS in advance of the declaration of thearray would be preferable because it might be calculated based uponsomething else, sourced from another file, or simply more obvious to thedeveloper that way, and the point of then using it in the declaration ofthe array is to stop the programmer forgetting to then initialise allthe elements.It does not necessarily stop the programmer from forgetting to initialiseall the elements, because there is not requirement for the compiler tocomplain at you for forgetting to initialise all of the elements. With an array, if you initialize at least one element, they all get initialized (but perhaps not to the desired values :-))This, indeed, was what I meant :-) I see. I'll need to bear this in mind for the future, but I'm reasonably sure most of the 5 or so C compilers I make a living from complain if the number of elements in the aggregate assignment doesn't match the size of the array. Will do some testing, if I ever get a spare moment... Nov 15 '05 #11

 P: n/a Stephen wrote: In article , Flash Gordon writesMike Wahler wrote: With an array, if you initialize at least one element,they all get initialized (but perhaps not to the desired values :-))This, indeed, was what I meant :-) I see. I'll need to bear this in mind for the future, but I'm reasonably sure most of the 5 or so C compilers I make a living from complain if the number of elements in the aggregate assignment doesn't match the size of the array. Will do some testing, if I ever get a spare moment... It's actually a very useful feature, since it means if you want to create an automatic structure initialised to zeros (perhaps it contains counts, sums, averages and pointers) you can do complex_type_t somevar = {0}; Far easier than either fully specifying it or looping over it. -- Flash Gordon Living in interesting times. Although my email address says spam, it is real and I read it. Nov 15 '05 #12

### This discussion thread is closed

Replies have been disabled for this discussion.