Function Called with Compatible Type?

Discussion in 'C Programming' started by Shao Miller, Dec 18, 2012.

  1. Shao Miller

    Shao Miller Guest

    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
    Shao Miller, Dec 18, 2012
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UmV6YSBOYWJp?=
    Replies:
    3
    Views:
    5,543
    =?Utf-8?B?ZWlsZWVuIG0=?=
    Jun 16, 2004
  2. =?Utf-8?B?TSBL?=
    Replies:
    1
    Views:
    21,638
    AbhijitSinha
    Nov 20, 2006
  3. christer-sandberg

    function called through a non-compatible type

    christer-sandberg, May 13, 2005, in forum: C Programming
    Replies:
    9
    Views:
    435
    Keith Thompson
    May 14, 2005
  4. mreister
    Replies:
    1
    Views:
    3,163
    mreister
    May 25, 2010
  5. pantagruel
    Replies:
    0
    Views:
    239
    pantagruel
    Feb 17, 2006
Loading...

Share This Page