Function Called with Compatible Type?

S

Shao Miller

So the following is undefined behaviour because 'x' changes between its
use for establishing the types of 'p1' and 'p2':

int main(void) {
int x = 42;
int arr1[x];
int (* p1)[x] = &arr1;

++x;
int arr2[x];
int (* p2)[x] = &arr2;

p1 = p2;

(void) p1;
return 0;
}

But iff the implementation supports VLAs, the following should be fine,
right?:

#include <stddef.h>
#include <stdio.h>

#define Countof(array) (sizeof (array) / sizeof *(array))

void myfunc(size_t n, int (* p1)[n], int (* p2)[n]) {
while (n--)
(*p1)[n] = (*p2)[n];
}

int main(int argc, char ** argv) {
(void) argv;

if (argc < 1)
return 0;
int x = argc;

typedef int arr_of_x[x];
typedef void f_alt_myfunc(size_t, arr_of_x *, arr_of_x *);

arr_of_x arr1;
arr_of_x arr2;

for (size_t i = 0; i < Countof(arr2); ++i)
arr2 = i + 1;

/* Ok? */
((f_alt_myfunc *) myfunc)(x, &arr1, &arr2);

printf("{ ");
for (int * p = arr1; p < arr1 + Countof(arr1); ++p)
printf("%d, ", *p);
printf("}\n");

return 0;
}

- Shao Miller
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top