1

I have the following versions of passing 2D array as pointer.

Version 1

#include <stdio.h>  

void disp(int a[][5])
{
    printf("a[0][3] = %d\n", a[0][3]); /* a[0][3] = 4 */
}

int main ()
{
    int a[10] = {1,2,3,4,5,6,7,8,9,10};
    disp(a);
    return 0;
}

Version 2

#include <stdio.h>

typedef void(*callDisplay)(int*);

void disp(int a[][5])
{
    printf("a[0][3] = %d\n", a[0][3]); /* a[0][3] = 4 */
}

int main ()
{
    int a[10] = {1,2,3,4,5,6,7,8,9,10};
    callDisplay fn = (callDisplay) &disp;
    fn(a);
    return 0;
}

Version 1 rises warning incompatible pointer type. expected int (*)[5] but argument is of type int * as expected. However, (Version 2) calling the same function with pointer is compiling without any such warnings.

gcc options: gcc -O0 -g3 -Wall -c -fmessage-length=0

Could somebody pass light on this?

0

2 Answers 2

2

If you remove the cast when assigning the function pointer you get:

tmp.c: In function ‘main’:
tmp.c:13:22: warning: initialization from incompatible pointer type [enabled by default]
     callDisplay fn = &disp;

The cast is suppressing this warning even though by casting to a function pointer of a different type you have invoked undefined behavior when you call the function pointer. Basically, you should never need to cast a function pointer as it will hide any warnings like this.

If you fix the function pointer you get the following code:

typedef void(*callDisplay)(int[][5]);

void disp(int a[][5])
{
    printf("a[0][3] = %d\n", a[0][3]); /* a[0][3] = 4 */
}

int main ()
{
    int a[10] = {1,2,3,4,5,6,7,8,9,10};
    callDisplay fn = &disp;
    fn(a);
    return 0;
}

Which when you compile you get the same warning as your first example:

tmp.c: In function ‘main’:
tmp.c:14:5: warning: passing argument 1 of ‘fn’ from incompatible pointer type [enabled by default]
     fn(a);
     ^
tmp.c:14:5: note: expected ‘int (*)[5]’ but argument is of type ‘int *’
Sign up to request clarification or add additional context in comments.

Comments

2

This function declaration

typedef void(*callDisplay)(int*);

has compatible argument when is called like

fn(a);

The problem is related to this casting

callDisplay fn = (callDisplay) &disp;

it is wrong.

That is the program has undefined behaviour.

According to the C Standard (6.3.2.3 Pointers)

8 A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer. If a converted pointer is used to call a function whose type is not compatible with the referenced type, the behavior is undefined.

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.