pointer to incomplete types

Discussion in 'C Programming' started by Michael Birkmose, May 14, 2004.

  1. Hi everyone!,


    Are pointers to incomplete types allowed in ANSI C?

    I can see that pointer arithmic on pointers to incomple types is
    impossible, however there are situations where it can be useful:

    consider this:

    void foo_function(int (*parm)[], int *parm_size);

    The idea is that the function takes a pointer to an array with an
    unspecified size of ints. Obviously pointer arithmics such as parm+1 is
    impossible. However accesing the value of the array pointed to by parm is
    possible: (*parm)[42].

    The reason for declaring the function like this is that it takes an array
    as a parameter, and outputs another array (potentially of a different
    size) as a paramter. Therefore the memory pointed to cannot merely be
    overwritten or realloced.

    The reason for me asking this question is that the program cdecl tells me
    that what I am doing is illegal

    cdecl>
    declare function (pointer to array of int, pointer to int )returning void
    Warning: Unsupported in C -- 'Pointer to array of unspecified dimension'
    (maybe you mean "pointer to object")
    void f(int (*)[], int *)


    And I also find it a bit odd that cdecl does not complain about the
    following:

    cdecl>
    declare function (pointer to array of array 10 of int, pointer to int
    )returning void
    void f(int (*)[][10], int *)


    Since an array of array 10 of int, also is an incomplete type? Did I miss
    something?

    --
    Michael Birkmose - stud.polyt
    Aalborg University - Department of Computer Science
    Fredrik Bajers Vej 7, B1-215
     
    Michael Birkmose, May 14, 2004
    #1
    1. Advertising

  2. Michael Birkmose

    Ben Pfaff Guest

    Michael Birkmose <> writes:

    > Are pointers to incomplete types allowed in ANSI C?


    Yes. For example, pointers to incomplete structure types are
    useful for abstraction, and pointers to void (which is an
    incomplete type that cannot be completed) are often seen as well.

    [...]
    > consider this:
    >
    > void foo_function(int (*parm)[], int *parm_size);

    [...]

    > The reason for me asking this question is that the program cdecl tells me
    > that what I am doing is illegal
    >
    > cdecl>
    > declare function (pointer to array of int, pointer to int )returning void
    > Warning: Unsupported in C -- 'Pointer to array of unspecified dimension'
    > (maybe you mean "pointer to object")
    > void f(int (*)[], int *)


    I believe that cdecl is wrong here. GCC doesn't complain, and I
    can't remember any standard prohibition against this construct.

    > And I also find it a bit odd that cdecl does not complain about the
    > following:
    >
    > cdecl>
    > declare function (pointer to array of array 10 of int, pointer to int
    > )returning void
    > void f(int (*)[][10], int *)


    I believe that this is also legitimate.
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
     
    Ben Pfaff, May 14, 2004
    #2
    1. Advertising

  3. Michael Birkmose

    Mitchell Guest

    On Fri, 14 May 2004 16:54:54 +0200, Michael Birkmose
    <> wrote:

    >Hi everyone!,
    >
    >
    >Are pointers to incomplete types allowed in ANSI C?
    >

    <snip>
    >cdecl>
    >declare function (pointer to array of int, pointer to int )returning void
    >Warning: Unsupported in C -- 'Pointer to array of unspecified dimension'
    > (maybe you mean "pointer to object")
    >void f(int (*)[], int *)


    That's odd. As Ben said, it is completely legal. "[]" is allowed in
    function declarations too, as stated in K&R. I tend to prefer "int *"
    in declarations as "[]" confuses me to the level of indirections
    especially in crazy things like "float ***".

    However... shouldn't "pointer to array of int" be written as "int []"
    or its equivalent "int *"? Thus the declaration cdecl returned seems
    wrong! What it returned seems to me like an array of pointers to int.

    Someone familiar with cdecl comment please?
     
    Mitchell, May 15, 2004
    #3
  4. Michael Birkmose

    Ben Pfaff Guest

    Mitchell <> writes:

    > However... shouldn't "pointer to array of int" be written as "int []"
    > or its equivalent "int *"? Thus the declaration cdecl returned seems
    > wrong! What it returned seems to me like an array of pointers to int.


    int a[]: Normally, an array of int, but when used as a parameter
    type it is interpreted as a pointer to int.

    int *b: Pointer to int.

    int *c[]: Normally, an array of pointers to int, but when used as
    a parameter type it is interpreted as a pointer to pointer to
    int.

    int (*d)[]: Pointer to array of int.
    --
    "I should killfile you where you stand, worthless human." --Kaz
     
    Ben Pfaff, May 15, 2004
    #4
  5. > However... shouldn't "pointer to array of int" be written as "int []"
    > or its equivalent "int *"? Thus the declaration cdecl returned seems
    > wrong! What it returned seems to me like an array of pointers to int.
    >
    > Someone familiar with cdecl comment please?


    int[] is const pointer to int I guess?

    However int (*foo)[] is pointer to int array (ie pointer to const
    pointer).

    So there is nothing wrong with the cdecl declaration I think.
    If you wanted to get an array of pointers to int you would do
    int *a[]

    Cheers,
    Michael
     
    Michael Birkmose, May 15, 2004
    #5
  6. Michael Birkmose

    Dan Pop Guest

    In <> Ben Pfaff <> writes:

    >Michael Birkmose <> writes:
    >
    >> Are pointers to incomplete types allowed in ANSI C?

    >
    >Yes. For example, pointers to incomplete structure types are
    >useful for abstraction, and pointers to void (which is an
    >incomplete type that cannot be completed) are often seen as well.
    >
    >[...]
    >> consider this:
    >>
    >> void foo_function(int (*parm)[], int *parm_size);

    >[...]
    >
    >> The reason for me asking this question is that the program cdecl tells me
    >> that what I am doing is illegal
    >>
    >> cdecl>
    >> declare function (pointer to array of int, pointer to int )returning void
    >> Warning: Unsupported in C -- 'Pointer to array of unspecified dimension'
    >> (maybe you mean "pointer to object")
    >> void f(int (*)[], int *)

    >
    >I believe that cdecl is wrong here. GCC doesn't complain, and I
    >can't remember any standard prohibition against this construct.


    The type is fine, but perfectly useless, as it cannot be dereferenced.
    If you need a generic pointer to array type, pointer to void is generic
    enough for the purpose.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, May 17, 2004
    #6
  7. On Sat, 15 May 2004 14:53:03 +0200, Michael Birkmose
    <> wrote:

    > > However... shouldn't "pointer to array of int" be written as "int []"
    > > or its equivalent "int *"? Thus the declaration cdecl returned seems
    > > wrong! What it returned seems to me like an array of pointers to int.
    > >
    > > Someone familiar with cdecl comment please?

    >
    > int[] is const pointer to int I guess?
    >

    No. Normally, it is an array (of unspecified bound) of int; you cannot
    _define_ (allocate) an object of that type (unless with an initializer
    that supplies the bound), and thus cannot declare an automatic object
    because such a declaration is always a definition; but you can use
    such a declaration to refer to an object defined suitably elsewhere.

    Any array lvalue (in C99 or rvalue!) when used in an expression with a
    few exceptions "decays" (converts) to a pointer _rvalue_; rvalues
    aren't qualified <OT> except in C++ for classes </> so it is neither
    const or non-const, but you can't assign to an rvalue and some people
    may think of that as 'const' or 'const-like' or 'constant'.

    _As a function parameter_ only, any top-level array declaration is
    "adjusted" or "rewritten" to pointer, so this declares a pointer to
    int. Not a const pointer; it can be reseated (assigned to point
    elsewhere) within its scope (the function body), although doing so may
    be confusing and therefore undesirable.

    See section 6 of the FAQ, at the usual places and
    http://www.eskimo.com/~scs/C-faq/top.html .

    > However int (*foo)[] is pointer to int array (ie pointer to const
    > pointer).
    >

    No, it's a pointer to array (of unspecified bound) of int. There is no
    pointer to pointer (qualified or not) anywhere. Treating pointer to
    array as if it were pointer to pointer, or vice versa, will produce
    completely bogus results and very likely traps.

    See section 6 of the FAQ.

    > So there is nothing wrong with the cdecl declaration I think.
    > If you wanted to get an array of pointers to int you would do
    > int *a[]
    >

    Now _that_ is right.

    - David.Thompson1 at worldnet.att.net
     
    Dave Thompson, May 27, 2004
    #7
  8. On 17 May 2004 16:31:44 GMT, (Dan Pop) wrote:

    > In <> Ben Pfaff <> writes:
    >
    > >Michael Birkmose <> writes:

    <snip>
    > >> cdecl> [complains]
    > >> declare function (pointer to array of int, pointer to int )returning void
    > >> Warning: Unsupported in C -- 'Pointer to array of unspecified dimension'
    > >> (maybe you mean "pointer to object")
    > >> void f(int (*)[], int *)

    > >
    > >I believe that cdecl is wrong here. GCC doesn't complain, and I
    > >can't remember any standard prohibition against this construct.

    >
    > The type is fine, but perfectly useless, as it cannot be dereferenced.


    Sure it can. It produces lvalue array unspecified bound of T, which
    type is compatible with the actual object type (assuming it is array
    of T) for the nominal "access", and as usual decays to pointer to T
    which can be used to access the elements. What you can't do is pointer
    arithmetic or (thus) subscript. Thus it is only equally useful as
    pointer to T, and more cluttered, and so rarely if ever preferable.
    But it is usable.

    - David.Thompson1 at worldnet.att.net
     
    Dave Thompson, May 27, 2004
    #8
    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. Replies:
    1
    Views:
    378
  2. Paul F. Dietz

    Question about incomplete array element types

    Paul F. Dietz, Jul 11, 2003, in forum: C Programming
    Replies:
    5
    Views:
    471
    Dan Pop
    Jul 11, 2003
  3. Replies:
    11
    Views:
    1,428
    James Kanze
    Jun 7, 2007
  4. Juha Nieminen
    Replies:
    13
    Views:
    649
    James Kanze
    Mar 2, 2008
  5. Juha Nieminen
    Replies:
    46
    Views:
    1,142
    Kai-Uwe Bux
    Sep 10, 2008
Loading...

Share This Page