Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Declaration of structs inside parameter list

Reply
Thread Tools

Declaration of structs inside parameter list

 
 
Michael Birkmose
Guest
Posts: n/a
 
      05-10-2004
Hi,

Using gcc the following is possible:

int some_function(struct local_struct { int member;} a);

This function takes one parameter "a" of the type struct local_struct.
This type is declared locally on the paramter list, and only has scope in
that function.

Gcc gives the following output:

warning: structure defined inside parms
warning: `struct local_struct' declared inside parameter list
warning: its scope is only this definition or declaration, which is probably not what you want.

My question is now - how would anyone invoke this function with such a
parameter? I mean it's hard to instantiate a variable of that type, since
it is not declared outside the function?

--
Michael Birkmose - stud.polyt
Aalborg University - Department of Computer Science
Fredrik Bajers Vej 7, B1-215
 
Reply With Quote
 
 
 
 
Kevin Bracey
Guest
Posts: n/a
 
      05-10-2004
In message <(E-Mail Removed) >
Michael Birkmose <(E-Mail Removed)> wrote:

> int some_function(struct local_struct { int member;} a);
>
> This function takes one parameter "a" of the type struct local_struct.
> This type is declared locally on the paramter list, and only has scope in
> that function.
>
> My question is now - how would anyone invoke this function with such a
> parameter? I mean it's hard to instantiate a variable of that type, since
> it is not declared outside the function?


You can't, which is why gcc is warning you that it's probably not what you
want.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
 
Reply With Quote
 
 
 
 
SM Ryan
Guest
Posts: n/a
 
      05-10-2004
Michael Birkmose <(E-Mail Removed)> wrote:
# Hi,
#
# Using gcc the following is possible:
#
# int some_function(struct local_struct { int member;} a);

#
# My question is now - how would anyone invoke this function with such a
# parameter? I mean it's hard to instantiate a variable of that type, since
# it is not declared outside the function?

struct another_struct {int member} x;
some_function(x);

This may be allowed: if the structs have the same members with
the same types, the structs can be considerred the same by some
compilers.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
We found a loophole; they can't keep us out anymore.
 
Reply With Quote
 
Martin Dickopp
Guest
Posts: n/a
 
      05-10-2004
SM Ryan <(E-Mail Removed)> writes:

> Michael Birkmose <(E-Mail Removed)> wrote:
> # Hi,
> #
> # Using gcc the following is possible:
> #
> # int some_function(struct local_struct { int member;} a);
>
> #
> # My question is now - how would anyone invoke this function with such a
> # parameter? I mean it's hard to instantiate a variable of that type, since
> # it is not declared outside the function?
>
> struct another_struct {int member} x;
> some_function(x);
>
> This may be allowed: if the structs have the same members with
> the same types, the structs can be considerred the same by some
> compilers.


No, the structs don't have compatible types, therefore they *cannot* be
considered the same by any conforming C implementation.

For the definition of type compatability, see section 6.2.7 of the
standard. 6.7.7#5 has a nice exaple:

| EXAMPLE 2: After the declarations
|
| typedef struct s1 { int x; } t1, *tp1;
| typedef struct s2 { int x; } t2, *tp2;
|
| type t1 and the type pointed to by tp1 are compatible. Type t1 is also
| compatible with type struct s1, but not compatible with the types
| struct s2, t2, the type pointed to by tp2, or int.

Martin


--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
 
Reply With Quote
 
Mitchell
Guest
Posts: n/a
 
      05-15-2004
On Mon, 10 May 2004 16:29:55 +0200, Martin Dickopp
<(E-Mail Removed)> wrote:

<snip>
>For the definition of type compatability, see section 6.2.7 of the
>standard. 6.7.7#5 has a nice exaple:
>
>| EXAMPLE 2: After the declarations
>|
>| typedef struct s1 { int x; } t1, *tp1;
>| typedef struct s2 { int x; } t2, *tp2;
>|
>| type t1 and the type pointed to by tp1 are compatible. Type t1 is also
>| compatible with type struct s1, but not compatible with the types
>| struct s2, t2, the type pointed to by tp2, or int.


I'm not too familiar with type compatibility, so let me check with
you. Are you saying that only instances of type declared at the /same/
statement are compatible? I may have gotten my terminologies wrong..

How about when it involved header files?

Thanks!
 
Reply With Quote
 
Martin Dickopp
Guest
Posts: n/a
 
      05-17-2004
Mitchell <(E-Mail Removed)> writes:

> On Mon, 10 May 2004 16:29:55 +0200, Martin Dickopp
> <(E-Mail Removed)> wrote:
>
> <snip>
>>For the definition of type compatability, see section 6.2.7 of the
>>standard. 6.7.7#5 has a nice exaple:
>>
>>| EXAMPLE 2: After the declarations
>>|
>>| typedef struct s1 { int x; } t1, *tp1;
>>| typedef struct s2 { int x; } t2, *tp2;
>>|
>>| type t1 and the type pointed to by tp1 are compatible. Type t1 is also
>>| compatible with type struct s1, but not compatible with the types
>>| struct s2, t2, the type pointed to by tp2, or int.

>
> I'm not too familiar with type compatibility, so let me check with
> you. Are you saying that only instances of type declared at the /same/
> statement are compatible? I may have gotten my terminologies wrong..


If two structures are declared with different tags, they don't have
compatible types, even if their members are the same.

> How about when it involved header files?


That makes no difference. Preprocessing is completed before the
implementation checks type compatability.

Martin


--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      05-17-2004
In article <news:(E-Mail Removed)>
Martin Dickopp <(E-Mail Removed)> writes:
>If two structures are declared with different tags, they don't have
>compatible types, even if their members are the same.


This rule is new in C99 (I think -- my dead-tree edition of the C89
standard is not easily accessible).

I am not really sure why the rule was added, although it is certainly
wise to use the same tag every time anyway.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      05-18-2004
In <(E-Mail Removed)> Chris Torek <(E-Mail Removed)> writes:

>In article <news:(E-Mail Removed)>
>Martin Dickopp <(E-Mail Removed)> writes:
>>If two structures are declared with different tags, they don't have
>>compatible types, even if their members are the same.

>
>This rule is new in C99 (I think -- my dead-tree edition of the C89
>standard is not easily accessible).


You're right. For C89:

3.1.2.6 Compatible type and composite type

Two types have compatible type if their types are the same.
Additional rules for determining whether two types are compatible are
described in $3.5.2 for type specifiers, in $3.5.3 for type
qualifiers, and in $3.5.4 for declarators. Moreover, two
structure, union, or enumeration types declared in separate
translation units are compatible if they have the same number of
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
members, the same member names, and compatible member types; for two
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
structures, the members shall be in the same order; for two
^^^^^^^^^^
enumerations, the members shall have the same values.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
type declaration in declaration of a parameter or return type of a function Luca Forlizzi C Programming 4 11-14-2010 09:30 PM
Using declaration inside first template parameter as default valuefor second template parameter. Stuart Redmann C++ 5 12-14-2007 08:42 AM
Array of structs instead of an array with pointers to structs? Paminu C Programming 5 10-11-2005 07:18 PM
const structs in other structs Chris Hauxwell C Programming 6 04-27-2004 07:03 PM
structs with fields that are structs Patricia Van Hise C Programming 5 04-05-2004 01:37 AM



Advertisments