for a global variable it returns the size of the array.
for a variable declared inside the same function it returns the size of the array.
for any other char array it returns the size of the pointer.
I have tried referencing and dereferencing to no avail.
to me this seems inconsistent with the other functions that operate on an array such as serial.println().
Two questions:
Am I the only one that thinks this is inconsistent ?
Is there anyway to get sizeof() to return the size of a char array passed to a function ?
(and yes I realize that I can write my own algorithm to search for a null)
2 is the correct and consistent and logical result!. Arrays are passed by location (a pointer) giving the pointer to sizeof() should logically return the size of a pointer not the size of the thing pointed to.
My apologies: I have yet to find a definitive definition for the difference between an operator and a function.
why (from wikipedia):
"When sizeof is applied to the name of a static array (not allocated through malloc), the result is the size in bytes of the whole array. This is one of the few exceptions to the rule that the name of an array is converted to a pointer to the first element of the array, and is possible just because the actual array size is fixed and known at compile time, when sizeof operator is evaluated. "
The reference guide indicates its use with an argument (in parentheses):
sizeof(xxx);
I missed the fact that it could be used without in parentheses.
I reread the wikipedia entry (I must learn to check there before posting a question here):
"If a type name is used, it must always be enclosed in parentheses, whereas expressions can be specified with or without parentheses."
void setup () {
int i;
Serial.begin (9600);
Serial.println(sizeof i);
// Serial.println(sizeof long); // will not compile
Serial.println(sizeof (long)); // for type sizeof requires parentheses
}
void loop() {}; // do nothing
holmes4:
size of is done at compile time not at run time. You got 10 and 6 for the first two sizeof()'s because "123456789" is a constant.
Mark
More to the point sizeof() works on the type of the value/variable, not its value, as the type is always known at
compile time. It can be applied directly to type expressions: sizeof (int)
What it all boils down to as far as I can tell is:
sizeof:
If one needs to determine the allocated size of an char array using sizeof then one must use the sizeof operator in the same function where the char array is declared and pass that parameter to other functions/operations as needed. The exception being a static char array declared at the global level.
operators: always return a value a returned value must be used
always require at least one parameter
may or may not require parentheses to enclose parameters
likely to be in a defined list somewhere in the ANSI "C" standard
functions:
may or may not return a value
a returned value may be used or it may be discarded
may or may not require parameter(s)
requires parentheses to enclose parameters
may be defined by ANSI "C", the compiler, the user or other source (external library)
KeithRB:
"++ is also a operator and its return value does NOT have to be used."
Which makes no sense, since there is no "return value" to be returned.
The pre- and post- increment/decrement operators are handy because they don't return a value.
They are also dangerous because they can lead to undefined behaviour when you do things like a*=i++;* [/quote] Is a return value defined to be a value generated by a function using the keyword "return" ?? What is the proper term to be used for the set of all values that may be used in an equation or statement that are generated by the inline use either an operator or function ?? I do see your point. It is actually the parameter that provides the value. ++ is merely a operation that is applied before or after the parameter is used. always return a value
... then it is a function, if it is used in the context of a function, such as:
bar = foo (42);
In that example I would assume bar is a variable and foo is a function.
I'm not sure that the "C" language, nor the compiler, defines a single function. The "standard" functions are part of the standard libraries, without those included (explicitly or implicitly) I doubt a single function would be known to the compiler. The linker would know about "main" because the code has to start somewhere.
The C standards (1990, 1999, and 201x) have two levels, freestanding and hosted.
In the hosted level, the standards defines many functions, which the compiler can assume what the function does (i.e. if you call memcpy, the compiler may decide to eliminate the call, and just do a couple of loads and stores).
In the freestanding level, most of the user functions are not defined, but there are a few special things that are still defined (such as va_start, va_end, etc. for dealing with variable arguments). Also, the compiler does assume that the internal functions it calls for complex code (such as floating point emulation) must still exist as well. In general, most of the embedded environments like Arduino, do still have some libraries for things like memcpy, even if they don't have an operating system.
I think that it may come down to a matter of semantics.
I would like to try and be semantically correct.
However it is a bit like what the cell phone lawyers have done in the US.
They have defined "unlimited" to be equal to 5 Gigabytes per month. (That was of course AFTER they had signed a bunch of customers up for unlimited accounts).
I think that it may come down to an operator being a special case of a function or a function being a special case of a operator.
I note that the Wikipedia table for operators lists a "function" as a operator.
I was rather hoping it would be the other way round ...
I would bet (not much) that ANSI "C" has a definitive list of "operators"
but at the high cost of ANSI standards it does not make for a very public reference.
You can always get Kernigham and Richie "The C programming Language" or Harbison and Steel "C:A reference manual" which will document anything you could want to know about C.
KeithRB:
You can always get Kernigham and Richie "The C programming Language" or Harbison and Steel "C:A reference manual" which will document anything you could want to know about C.
K&R (2nd edition)was of no help. In the preface they mention that the book assumes a certain familiarity with programing and related terms (specifically mentioned is "function"). The body then goes on to define a great number of operators and the use of functions but does not define either term. It also refers the reader back to the ANSI C standard for a definitive reference.
Wikapeadia had an interesting entry under operator:
Syntactically operators usually contrast to functions. In most languages, functions may be seen as a special form of prefix operator with fixed precedence level and associativity, often with compulsory parentheses e.g. Func(a) (or (Func a) in LISP). Most languages support programmer-defined functions, but cannot really claim to support programmer-defined operators, unless they have more than prefix notation and more than a single precedence level. Semantically operators can be seen as special form of function with different calling notation and a limited number of parameters (usually 1 or 2).
Nothing like riding the fence to confuse an issue ....
So for until I find something better I am going to stick with:
An operator is a special form of a function that is predefined by the language definition/implementation to be an operator.