Hello i am new to this C programs

S

siva

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},
};
 
V

venkatesh

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
 
S

Stephen

siva said:
const 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},
};
 
F

Flash Gordon

siva said:
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.
 
S

Stephen

Flash Gordon said:
#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.
 
F

Flash Gordon

Stephen said:
Flash Gordon said:
#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.
 
M

Mike Wahler

Flash Gordon said:
Stephen said:
Flash Gordon said:
#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
 
F

Flash Gordon

Mike said:
Stephen said:
#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 :))

This, indeed, was what I meant :)
 
S

Stephen

Flash Gordon said:
Mike said:
Stephen wrote:

#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 :))

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...
 
F

Flash Gordon

Stephen said:
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.
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top