macro question

O

osmium

Bill Cunningham said:
osmium wrote:

I have kandr2 and I will check that page but I have trouble
understanding kandr2. I have learned more from these sporatic tutorials
online.

I suggest

Page 155 tells you that the sequence ... has a particular meaning in the C
language, your post made it difficult to tell whether you were talking C or
talking English - they kind of blur together into a pudding.

It is clear now that you were merely wondering how to write the parameter
for an array of doubles. Given your current problem I would write it as
foo(double x[]);
but
foo(double* x);
would have compiled OK too. The first form tells the reader that x indeed
*is* an array. For a one dimensional array there is no need to put a number
in the brackets. But once you put a number there, the compiler is allowed
to (and mine does) check it for validity. DBL_MAX is the wrong type
(floating point instead of integral type and then if this were fixed the
number is much too big to be sensible. The pointer form (double* x) gives no
information as to what use x might have, it is simply a pointer.

It *can* make sense to put a number in there as a hint to the reader as to
what is going on, for example:

goo(int day[7]);
or
hoo(card[52];
contain helpful hints for the alert reader.

In your case no helpful hint seems possible, so I would leave the brackets
empty (as I did above).

The number given in the *prototype* will not actually be used by the
compiler, nevertheless it can cause warning messages.

The above assumes you were indeed writing a prototype, or function
declaration, since you didn't get as far as a semi-colon, we are left
guessing as to your plans.
 
K

Keith Thompson

osmium said:
It is clear now that you were merely wondering how to write the parameter
for an array of doubles. Given your current problem I would write it as
foo(double x[]);
but
foo(double* x);
would have compiled OK too. The first form tells the reader that x indeed
*is* an array. For a one dimensional array there is no need to put a number
in the brackets. But once you put a number there, the compiler is allowed
to (and mine does) check it for validity.

It checks it for validity only in the sense that it confirms that it's of
some integer type. The value is ignored.

All compilers *must* make this check; the declaration
void foo(double x[1.5]);
is a constraint violation, and must at least trigger a diagnostic.
DBL_MAX is the wrong type
(floating point instead of integral type and then if this were fixed the
number is much too big to be sensible. The pointer form (double* x) gives no
information as to what use x might have, it is simply a pointer.

And I would tend to argue that this is more accurate. It really is
simply a pointer.

The counterargument is that using the [] syntax encourages the caller to
pass (a pointer to the first element of) an array -- but that's what
comments are for.

My preference for (param *type) rather than (param type[]) is personal;
if you think (param type[]) is clearer, that's fine.
It *can* make sense to put a number in there as a hint to the reader as to
what is going on, for example:

goo(int day[7]);
or
hoo(card[52];
contain helpful hints for the alert reader.

But a trap for the somewhat less alert reader; for example, it may be
easier to forget to check whether an argument is a null pointer if it's
declared with array syntax.
In your case no helpful hint seems possible, so I would leave the brackets
empty (as I did above).

The number given in the *prototype* will not actually be used by the
compiler, nevertheless it can cause warning messages.

Does your compiler issue a warning based on the *value* of the number,
or does it just verify that it's of some integer type?
 
O

osmium

Keith Thompson said:
osmium said:
It is clear now that you were merely wondering how to write the parameter
for an array of doubles. Given your current problem I would write it as
foo(double x[]);
but
foo(double* x);
would have compiled OK too. The first form tells the reader that x
indeed
*is* an array. For a one dimensional array there is no need to put a
number
in the brackets. But once you put a number there, the compiler is
allowed
to (and mine does) check it for validity.

It checks it for validity only in the sense that it confirms that it's of
some integer type. The value is ignored.

I use DevC and putting a large integer constant in the braces in a function
declaration complains if it is too large. Which seems to me like exactly
the right thing to do. To do otherwise is like hiding dirt under the rug.
 
K

Keith Thompson

osmium said:
Keith Thompson said:
osmium said:
It is clear now that you were merely wondering how to write the parameter
for an array of doubles. Given your current problem I would write it as
foo(double x[]);
but
foo(double* x);
would have compiled OK too. The first form tells the reader that x
indeed
*is* an array. For a one dimensional array there is no need to put a
number
in the brackets. But once you put a number there, the compiler is
allowed
to (and mine does) check it for validity.

It checks it for validity only in the sense that it confirms that it's of
some integer type. The value is ignored.

I use DevC and putting a large integer constant in the braces in a function
declaration complains if it is too large. Which seems to me like exactly
the right thing to do. To do otherwise is like hiding dirt under the rug.

A bit of Googling indicates that DevC uses gcc.

Experiment suggests that gcc prints an error message for

void foo(some_type arr[SOME_INTEGER_CONSTANT])
{
}

if the resulting size would exceed SIZE_MAX bytes. Probably the code
that emits that warnings is the same used for an array object
declaration.

Personally, I'd much rather have a warning that the expression will
be ignored altogether.
 
K

Keith Thompson

Kenneth Brody said:
I'm a little confused here. DBL_MAX is for a double isn't it? Why are
you mentioning integers? Isn't that what int stands for?

He's referring to this line in your original post:

double ema(double ma1[DBL_MAX]... and so on

The C standard says that array sizes must be expressed in an integer type.
DBL_MAX is too large to be stored in any integer type and is therefore not
suitable for expressing an array size.

Is this guaranteed? Can an implementation define "long long" to be large
enough to hold DBL_MAX, and still be conforming?

No, and yes, respectively. Though it's unlikely, an implementation can
legally make LLONG_MAX or ULLONG_MAX (or, perhaps more likely,
UINTMAX_MAX) larger in magnitude than DBL_MAX. DBL_MAX is required to
be at least 1E+37, so a 128-bit integer type could be big enough (though
most implementations make DBL_MAX considerably bigger).

But, as I've already pointed out, the real problem is that
DBL_MAX is of the wrong type, regardless of its magnitude.
"double ma1[DBL_MIN]" or "double ma1[3.0]" is a constraint violation
for exactly the same reason.
 
G

Guest

[...]
It is clear now that you were merely wondering how to write the parameter
for an array of doubles.  Given your current problem I would write itas
 foo(double x[]);
    but
 foo(double* x);
 would have compiled OK too.  The first form tells the reader that x indeed
*is* an array.  For a one dimensional array there is no need to put anumber
in the brackets.  But once you put a number there, the compiler is allowed
to (and mine does) check it for validity.

It checks it for validity only in the sense that it confirms that it's of
some integer type.  The value is ignored.

N1256 "6.7.5.2 Array declarators
Constraints
1 ... If the expression is a constant expression, it shall have a
value greater than zero."
 

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,952
Messages
2,570,111
Members
46,695
Latest member
Juliane58C

Latest Threads

Top