Connecting Tech Pros Worldwide Forums | Help | Site Map

pointer to incomplete types

Michael Birkmose
Guest
 
Posts: n/a
#1: Nov 14 '05
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

Ben Pfaff
Guest
 
Posts: n/a
#2: Nov 14 '05

re: pointer to incomplete types


Michael Birkmose <birkmose@cs.auc.dk> writes:
[color=blue]
> Are pointers to incomplete types allowed in ANSI C?[/color]

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.

[...][color=blue]
> consider this:
>
> void foo_function(int (*parm)[], int *parm_size);[/color]
[...]
[color=blue]
> 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 *)[/color]

I believe that cdecl is wrong here. GCC doesn't complain, and I
can't remember any standard prohibition against this construct.
[color=blue]
> 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 *)[/color]

I believe that this is also legitimate.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}
Mitchell
Guest
 
Posts: n/a
#3: Nov 14 '05

re: pointer to incomplete types


On Fri, 14 May 2004 16:54:54 +0200, Michael Birkmose
<birkmose@cs.auc.dk> wrote:
[color=blue]
>Hi everyone!,
>
>
>Are pointers to incomplete types allowed in ANSI C?
>[/color]
<snip>[color=blue]
>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 *)[/color]

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?
Ben Pfaff
Guest
 
Posts: n/a
#4: Nov 14 '05

re: pointer to incomplete types


Mitchell <cheeHATESPAMliang@inaHATESPAMme.coHATESPAMm> writes:
[color=blue]
> 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.[/color]

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
Michael Birkmose
Guest
 
Posts: n/a
#5: Nov 14 '05

re: pointer to incomplete types


> However... shouldn't "pointer to array of int" be written as "int []"[color=blue]
> 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?[/color]

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
Dan Pop
Guest
 
Posts: n/a
#6: Nov 14 '05

re: pointer to incomplete types


In <87d656zyvb.fsf@blp.benpfaff.org> Ben Pfaff <blp@cs.stanford.edu> writes:
[color=blue]
>Michael Birkmose <birkmose@cs.auc.dk> writes:
>[color=green]
>> Are pointers to incomplete types allowed in ANSI C?[/color]
>
>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.
>
>[...][color=green]
>> consider this:
>>
>> void foo_function(int (*parm)[], int *parm_size);[/color]
>[...]
>[color=green]
>> 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 *)[/color]
>
>I believe that cdecl is wrong here. GCC doesn't complain, and I
>can't remember any standard prohibition against this construct.[/color]

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@ifh.de
Dave Thompson
Guest
 
Posts: n/a
#7: Nov 14 '05

re: pointer to incomplete types


On Sat, 15 May 2004 14:53:03 +0200, Michael Birkmose
<birkmose@cs.auc.dk> wrote:
[color=blue][color=green]
> > 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?[/color]
>
> int[] is const pointer to int I guess?
>[/color]
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 .
[color=blue]
> However int (*foo)[] is pointer to int array (ie pointer to const
> pointer).
>[/color]
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.
[color=blue]
> 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[]
>[/color]
Now _that_ is right.

- David.Thompson1 at worldnet.att.net
Dave Thompson
Guest
 
Posts: n/a
#8: Nov 14 '05

re: pointer to incomplete types


On 17 May 2004 16:31:44 GMT, Dan.Pop@cern.ch (Dan Pop) wrote:
[color=blue]
> In <87d656zyvb.fsf@blp.benpfaff.org> Ben Pfaff <blp@cs.stanford.edu> writes:
>[color=green]
> >Michael Birkmose <birkmose@cs.auc.dk> writes:[/color][/color]
<snip>[color=blue][color=green][color=darkred]
> >> 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 *)[/color]
> >
> >I believe that cdecl is wrong here. GCC doesn't complain, and I
> >can't remember any standard prohibition against this construct.[/color]
>
> The type is fine, but perfectly useless, as it cannot be dereferenced.[/color]

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