473,651 Members | 3,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(in t (*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 8668
Michael Birkmose <bi******@cs.au c.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(in t (*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[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof 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.au c.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************ ***@inaHATESPAM me.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.o rg> Ben Pfaff <bl*@cs.stanfor d.edu> writes:
Michael Birkmose <bi******@cs.au c.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(in t (*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.au c.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.ne t
Nov 14 '05 #7
On 17 May 2004 16:31:44 GMT, Da*****@cern.ch (Dan Pop) wrote:
In <87************ @blp.benpfaff.o rg> Ben Pfaff <bl*@cs.stanfor d.edu> writes:
Michael Birkmose <bi******@cs.au c.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.ne t
Nov 14 '05 #8

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

Similar topics

22
2202
by: Neo | last post by:
Hi Folks, #include<stdio.h> int main() { int (*p); int arr; int i;
204
12982
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 = {0,1,2,4,9};
16
2502
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
41
10018
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 integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.55) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed...
98
6391
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 "right" way to go about calling malloc instead is to dereference a pointer; apparently even if said pointer is undefined. E.g. ptr = malloc(sizeof(struct some_struct)); = wrong ptr = malloc(*ptr); = right
50
4469
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): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
8361
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8278
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7299
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1912
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.