473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

pointer to incomplete types

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
Nov 14 '05 #1
7 8614
Michael Birkmose <bi******@cs.auc.dk> 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[]="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;}
Nov 14 '05 #2
On Fri, 14 May 2004 16:54:54 +0200, Michael Birkmose
<bi******@cs.auc.dk> 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?
Nov 14 '05 #3
Mitchell <ch***************@inaHATESPAMme.coHATESPAMm> 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
Nov 14 '05 #4
> 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
Nov 14 '05 #5
In <87************@blp.benpfaff.org> Ben Pfaff <bl*@cs.stanford.edu> writes:
Michael Birkmose <bi******@cs.auc.dk> 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: Da*****@ifh.de
Nov 14 '05 #6
On Sat, 15 May 2004 14:53:03 +0200, Michael Birkmose
<bi******@cs.auc.dk> 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
Nov 14 '05 #7
On 17 May 2004 16:31:44 GMT, Da*****@cern.ch (Dan Pop) wrote:
In <87************@blp.benpfaff.org> Ben Pfaff <bl*@cs.stanford.edu> writes:
Michael Birkmose <bi******@cs.auc.dk> 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
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Neo | last post by:
Hi Folks, #include<stdio.h> int main() { int (*p); int arr; int i;
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
98
by: Micheal Smith | last post by:
I recently read an article containing numerous gripes about common C practices. One of them contained a gripe about the use of the sizeof operator as an argument to malloc calls. The supposed...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.