Compatible types and argument type qualifiers
Asked Answered
C

3

5

Are the types of these two declarations compatible types?

void f(char *, char *);
void f(char *restrict, char *restrict);

or similarly:

void g(char *);
void g(char *const);

I'm having a hard time finding anything in the standard which covers the issue. I'm mostly interested in the topic of whether it's valid to manually prototype a function, omitting the restrict keyword, where the actual type might have restrict-qualified arguments depending on the version of C or version of other libraries in use.

Capitalize answered 13/8, 2013 at 17:38 Comment(1)
yes, these are compatible; goes digging up the standard quoteReprise
R
4

C11 section 6.7.6.3 §15:

In the determination of type compatibility and of a composite type, each parameter declared with function or array type is taken as having the adjusted type and each parameter declared with qualified type is taken as having the unqualified version of its declared type.

Reprise answered 13/8, 2013 at 17:50 Comment(5)
I had a hard time determining which answer to accept, but I saw the "38 mins ago" change to "39 mins ago" on your answer just before it did on ouah's answer, so this one's yours. :-)Capitalize
@R.. You can hover over the timestamp on the answers and see the exact times the answers were posted. Actually ouah posted his answer 18 secs before Christoph did. The choice is yours of course ;-)Gratitude
@KingsIndian: in my defence, my comment predates both his and my answersReprise
@Reprise I don't know what you are defending against. It was in no way intended to offend you :) R.. seem to imply that you posted before ouah which is why I commented. I was also aware that you posted your comment before both of you posted the same answers. +1 to you as well if it makes the day better ;-)Gratitude
@KingsIndian: that was tongue-in-cheek, but feel free to upvote to avoid hurting my feelings anyway :pReprise
C
5

They are compatible:

(C99, 6.7.5.3 Function declarators (including prototypes) p15) "[...] (In the determination of type compatibility and of a composite type, each parameter declared with function or array type is taken as having the adjusted type and each parameter declared with qualified type is taken as having the unqualified version of its declared type.)"

Cymograph answered 13/8, 2013 at 17:50 Comment(2)
Apologies for my incorrect determination of who posted first. ;-) At least @Reprise did comment first, though.Capitalize
@R.. no problem, we were really close.Cymograph
R
4

C11 section 6.7.6.3 §15:

In the determination of type compatibility and of a composite type, each parameter declared with function or array type is taken as having the adjusted type and each parameter declared with qualified type is taken as having the unqualified version of its declared type.

Reprise answered 13/8, 2013 at 17:50 Comment(5)
I had a hard time determining which answer to accept, but I saw the "38 mins ago" change to "39 mins ago" on your answer just before it did on ouah's answer, so this one's yours. :-)Capitalize
@R.. You can hover over the timestamp on the answers and see the exact times the answers were posted. Actually ouah posted his answer 18 secs before Christoph did. The choice is yours of course ;-)Gratitude
@KingsIndian: in my defence, my comment predates both his and my answersReprise
@Reprise I don't know what you are defending against. It was in no way intended to offend you :) R.. seem to imply that you posted before ouah which is why I commented. I was also aware that you posted your comment before both of you posted the same answers. +1 to you as well if it makes the day better ;-)Gratitude
@KingsIndian: that was tongue-in-cheek, but feel free to upvote to avoid hurting my feelings anyway :pReprise
I
1

The names of the arguments in the prototype doesn't matter, so these definitions are equivalent. However it's a good practice, to put the names, as these should give some idea what the arguments are intended for. Technically they are not need though, but serve as a documentation.

It is a different matter with the constqualifier, because this changes the meaning of the function.

Illboding answered 13/8, 2013 at 17:43 Comment(3)
These are no argument names in my argument lists, only type qualifiers.Capitalize
And it's not clear that the const changes the "signature" of the function. The const does not apply to the pointed-to type. It applies to the automatic-storage object that receives the value of the argument internal to the function, which is an implementation detail of the function, not part of its interface contract.Capitalize
@R.., You are right, signature is the wrong word, I changed it to meaning, which also seems wrong, but I don't know how to better put it in english.Illboding

© 2022 - 2024 — McMap. All rights reserved.