Home My Page Projects Modern C
Summary Activity SCM

Forum: discussion

Monitor Forum | Start New Thread Start New Thread
RE: Confused about Takeaway [ Reply ]
By: Jens Gustedt on 2020-08-11 09:48

Yes, this is probably one of the most confusing parts of C, how array parameters work.

At that point of the book you probably should take what is written here literally. Because this is a function parameter, the rules for empty `[]` are relaxed and they may appear as indicated.

When we use them in a definition of an object, however, this set of empty `[]` is "completed" by what is given in the initializer. In that case the largest index that is used is 3, and so `E` has 4 elements.

And yes, for function parameters this also refers to the fact that `sizeof` for array parameters does not do what we would usually expect when we use it for arrays.

At a point in the book where there we do not have "pointers", yet, these special rules for array parameters are unfortunately all that can be said. Once you move forward to these more involved topics of pointers (and so-called rewriting of array parameters to pointers) things will hopefully become clearer. Until then, just stick to something like

array parameters for functions are special

not all that can be done with a "normal" array can be done with an array parameter


Confused about Takeaway [ Reply ]
By: Nobody on 2020-08-11 05:55

Sorry if this was asked.

"As we saw for the prototype of printf, such parameters may have empty []. Since no initializer is possible for such a parameter, the array dimension can’t be determined."

However on the previous page you initialize with empty [] using the initializer's length? For example:

double E[] = { [3] = 42.0, [2] = 37.0, };

Also, the book claims the "innermost" dimension is lost. Is this referring to when you pass to a function some multidimensional array, sizeof cannot be used on the array holding the arrays? That feels confusing.

Otherwise I am enjoying and thanks so much.