pointer to incomplete types

M

Michael Birkmose

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

Ben Pfaff

Michael Birkmose said:
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.
 
M

Mitchell

Hi everyone!,


Are pointers to incomplete types allowed in ANSI C?
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?
 
B

Ben Pfaff

Mitchell said:
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.
 
M

Michael Birkmose

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
 
D

Dan Pop

In said:
Michael Birkmose said:
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
 
D

Dave Thompson

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
 
D

Dave Thompson

In said:
Michael Birkmose <[email protected]> writes:
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
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top