1

I have a question regarding to the code snippet appended below. Anyway I ran the snippet on ideone.com and got two different results

  • C: Succeed.
  • C++: Error:
    prog.cpp: In function ‘int main()’:
    prog.cpp:20:13: error: cannot convert ‘int* (*)[2][10]’ to \
        ‘int* (*)[10]’ for argument ‘1’ to ‘void foo(int* (*)[10], size_t)’
        foo(&a, LEN);
                   ^
    

The result in C++ is what I expect, but it runs successfully in C, and it seems like it's compiler dependent because people on the chat helping ran the snippet only got a warning.

So which part I've missed? Is that C automatically did some conversion?


#include <stdio.h>
#include <stddef.h>
#define LEN 2

void foo(int* a[][10], size_t len) {
    printf("%s\n", "successfully called foo.");
}

int main(void) {

    // a is an LEN-array of an 10-array of (int *)
    int *a[LEN][10] = {{0}};
    // but the identifier `a` will decay to be a pointer of type int*[10]

    // p1 is a pointer to an 10-array of (int *)
    int *(*p1)[10] = 0;

    foo(a, LEN);
    foo(&a, LEN);

    return 0;
}
6
  • It is not really successful, your compiler should at least give you a warning. Commented Nov 20, 2018 at 16:02
  • 1
    Compiling with adequate C flags gives error: incompatible pointer types passing 'int *(*)[2][10]' to parameter of type 'int *(*)[10]'. Commented Nov 20, 2018 at 16:02
  • 1
    ideone.com/iv3MXi gives you the correct error. You should choose C99 instead of C at ideone. Commented Nov 20, 2018 at 16:03
  • 3
    Legacy C is traditionally very lenient when it comes to type conversion. C++ is much more strict about. With adequate level of warnings, C will also warn you. Commented Nov 20, 2018 at 16:04
  • 3
    On a side note, this might be the first time when I see both tags C and C++ correctly used in the single question! Commented Nov 20, 2018 at 16:05

2 Answers 2

4

Drastic edit; previous answer was wrong as pointed out in the comments.

The program is ill-formed in both C and C++. But the standards of the respective languages don't disallow successfully compiling programs that violate the imposed constraints. This allows the implementations to extend the language. Implementations are merely required to issue a diagnostic message. Both a warning and an error are conforming behaviours.

For whatever reason, the compiler that you use (through ideone) has chosen to behave differently when compiling C++.

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

3 Comments

The fact that any pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type does not mean implicit conversions are well-formed or will be performed. C 2018 6.5.2.2 8 says implicit conversions will not be performed for function call arguments other than as previously listed, and 7 says arguments for this function call with a prototype will be converted as if by assignment, and 6.5.16.1 1 (for assignment) says this conversion violates constraints. So, no, it is not a well-formed conversion, regardless of aliasing.
@EricPostpischil hmm, you're right. I know more of C++ than C. Your argument appears to hold all the way back to C89.
@EricPostpischil I rewrote the answer.
4

This is not valid in C. Using gcc with -Wall -Wextra, it outputs the following:

x1.c: In function ‘main’:
x1.c:19:9: warning: passing argument 1 of ‘foo’ from incompatible pointer type [-Wincompatible-pointer-types]
     foo(&a, LEN);
         ^
x1.c:5:6: note: expected ‘int * (*)[10]’ but argument is of type ‘int * (*)[2][10]’
 void foo(int* a[][10], size_t len) {
      ^~~

The types are not compatible. It only shows up as a warning because C tends to allow various pointer conversions even though they aren't proper.

You can however do this:

int *(*p1)[10] = a;

foo(a, LEN);
foo(p1, LEN);

5 Comments

Just enabling -pedantic-errors will make it an error, as it should.
if it is a warning then it is not invalid. -Wall -Wextra rejects code that strictly speaking is valid afaik
you are both right. The code is syntactically correct, but semantically incorrect. Warnings usually warn on syntactically correct code that has high probability of being semantically incorrect or have unexpected semantics.
@user463035818: “Valid” and “invalid” are not well defined terms in the C standard. For a function call with a prototype, the arguments are converted as if by assignment (C 2018 6.5.2.2 7), and no other conversions are performed implicitly (8). For assignment, this violates the constraints (6.5.16.1 1), as the only acceptable assignment of a non-void pointer to a non-void pointer is for compatible types (with some flexibility on qualifiers).
@EricPostpischil yes, they are not well defined, though if I read your comment correctly the code is "not ok" according to the C standard. Actually I dont remember what was making me write that comment, maybe i misread the answer as implying getting a warning means the code is invalid, which the answer does actually not claim. Anyhow, thanks for the details

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.