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
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