5

Below is the code snippet. I want to know if line no. 17 typecasting is valid and proper in c?

#include <stdio.h>

typedef int twoInts[2];

void print(twoInts *twoIntsPtr);
void intermediate (twoInts twoIntsAppearsByValue);

int main () {
    twoInts a;
    a[0] = 0;
    a[1] = 1;
    print(&a);
    intermediate(a);
    return 0;
}
void intermediate(twoInts b) {
    print((int(*)[])b); // <<< line no. 17 <<<
}

void print(twoInts *c){
    printf("%d\n%d\n", (*c)[0], (*c)[1]);
}

Also, when i change the definition intermediate to

void intermediate(twoInts b) {
    print(&b);
}

I am getting below warnings while compiling and o/p is not proper.

1.c:17:11: warning: passing argument 1 of print from incompatible pointer type
     print(&b);
           ^
1.c:5:6: note: expected int (*)[2] but argument is of type int **
 void print(twoInts *twoIntsPtr);

As per my understanding, array is decaying to pointer to int in funtion argument. What is the exact reason?

3
  • 3
    Let me just add that: void print(twoInts *twoIntsPtr); is identical to: void print(int (*twoIntsPtr)[2]); Or in other words, don't hide arrays and pointers behind typedefs. Commented Oct 14, 2015 at 10:51
  • 2
    The best and proper solution to the problem, is indeed to never hide an array behind a typedef. Then you could use plain int*pointers without the need to obfuscate the program to oblivion. Commented Oct 14, 2015 at 11:19
  • I'd say print((twoInts *)&b); should do. Commented Oct 14, 2015 at 15:14

1 Answer 1

3

a is an array. So when you pass it to intermediate(), it gets converted into a pointer to its first element (aka "array decaying").

So, the b in:

void intermediate(twoInts b) {
    print(&b);
}

has type int*, not int[2] as you might seem to expect. Hence, &b is of type int** rather than int (*)[2] that's expected by print() function. Hence, the types don't match.

If you change intermediate() to:

void intermediate(twoInts *b) {
    print(b);
}

then you wouldn't need to pass the address of the &b and it will as expected and the types would match correctly.

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

Comments

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.