2

Is there a difference between:

void draw_line(float p0[2], float p1[2], float color[4]);

and this:

void draw_line(float *p0, float *p1, float *color);

in C?

1 Answer 1

3
  1. List item

There is no difference between the function declarations neither in C nor in C++.

A function parameter having an array type is implicitly adjusted by the compiler to pointer to the array element type.

From the C Standard (6.7.6.3 Function declarators (including prototypes))

7 A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation...

Thus these declarations

void draw_line(float p0[2], float p1[2], float color[4]);

void draw_line(float *p0, float *p1, float *color);

declare the same one function and the both can be present in a program though the compiler can issue a message that there are redundant declarations.

The difference between C and C++ is that in C you can specify in brackets qualifiers and an expression with the keyword static that specifies the number of elements the arguments shall provide the access to.

  1. ...If the keyword static also appears within the [ and ] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression.

In the both C and C++ an array used as an argument of such a parameter is also implicitly converted to pointer to its first element.

Sign up to request clarification or add additional context in comments.

6 Comments

So what's the point on doing float p0[2] ? Just more context for the caller of that function and nothing more?
@FrameBuffer It is for selfdocumentation. Also in C you can use the keyword static inside brackets to specify that the argument shall provide access to the specified number of elements.
Another relevant paragraph of the standard should be port70.net/~nsz/c/c11/n1570.html#6.9.1p10 : "On entry to the function, the size expressions of each variably modified parameter are evaluated and the value of each argument expression is converted to the type of the corresponding parameter as if by assignment. (Array expressions and function designators as arguments were converted to pointers before the call.)"
As for "the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression", my knowledge says that it is (1) something that the compiler doesn't check, (2) an information (the size) that won't be available inside the function. I'm actually asking this because of the comments in this answer
@RobertoCaboni Truly speaking I did not use this feature myself.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.