[comp.lang.c] Struct definition in MS-C

shuang@caip.rutgers.edu (Shuang Chen) (08/17/89)

I am trying to use struct to implement a linked list with Microsoft C.
The problem I had was that I could not define a node structure with a
pointer to point to another node of the same type.  The program was

typedef struct {

	...
	...
	node *next;
	} node;


Actually I tried to use

struct node {
	
	...
	...
	struct node *next;
	};
as it is with standard C, but this doesn't work with MS-C.
	
Does any one have such experience ?  I appriciate any help since I
have to implement this on a IBM PC/AT with Microsoft C.

Thanks...	

			
				Shuang

fredex@cg-atla.UUCP (Fred Smith) (08/17/89)

In article <Aug.16.14.48.38.1989.11505@caip.rutgers.edu> shuang@caip.rutgers.edu (Shuang Chen) writes:
>
>
>I am trying to use struct to implement a linked list with Microsoft C.
>The problem I had was that I could not define a node structure with a
>pointer to point to another node of the same type.  The program was
>
>typedef struct {
>
>	...
>	...
>	node *next;
>	} node;
>
>



------------------------

I have done exactly what you want by doing the following, which is
much like examples in K&R (1st edition) page 131 and page 140:

typedef struct node
	{
	...
	...
	node *next;
	} NODE, *PNODE;

After doing this is is possible to declare data entities of
type NODE, which allocates the space for such a structure, and
as type PNODE, which allocates space for a pointer to such
a structure.

This works. I have done it numerous times. The problem with the
example you gave (reproduced above) is that the structure definition
contains an item of type pointer to node, but at that point the
compiler does not yet know what a node is. I suggest you check out
chapter 6 of K&R (1st Ed) for examples and discussion.

Good luck!

Fred Smith

gwyn@smoke.BRL.MIL (Doug Gwyn) (08/17/89)

In article <Aug.16.14.48.38.1989.11505@caip.rutgers.edu> shuang@caip.rutgers.edu (Shuang Chen) writes:
>struct node {
>	struct node *next;
>	};
>as it is with standard C, but this doesn't work with MS-C.

Actually that should have worked with anybody's C.
Try something like
	struct node;
	struct node {
		struct node *next;
	};
If that doesn't work either, consider changing compiler vendors.

jeffa@hpmwtd.HP.COM (Jeff Aguilera) (08/18/89)

> struct node {
> 	
> 	...
> 	...
> 	struct node *next;
> 	};
> as it is with standard C, but this doesn't work with MS-C.

Are you SURE that doesn't work?  MS-C handles my linked lists
perfectly.  

You might try the following:

	typedef struct node *Node;

	struct node {
		...
		Node next;
	};

bill@twwells.com (T. William Wells) (08/18/89)

In article <10761@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn) writes:
: In article <Aug.16.14.48.38.1989.11505@caip.rutgers.edu> shuang@caip.rutgers.edu (Shuang Chen) writes:
: >struct node {
: >     struct node *next;
: >     };
: >as it is with standard C, but this doesn't work with MS-C.

Baloney. It works fine with Microsoft C.

: Actually that should have worked with anybody's C.
: Try something like
:       struct node;
:       struct node {
:               struct node *next;
:       };

Eh? The first example is perfectly good C. The only case I think it
might fail is with something like:

struct node {
	...
};
foo()
{
	struct node {
		struct node *next;
	};

The "struct node *" is, I think, going to refer to the global struct
node, not the local one; in that case, adding an additional "struct
node;" at the start of the function will fix things.

---
Bill                    { uunet | novavax | ankh | sunvice } !twwells!bill
bill@twwells.com

sam_hr@tor.nhh.no (Heine Rasmussen) (08/19/89)

In article <7529@cg-atla.UUCP>, fredex@cg-atla.UUCP (Fred Smith) writes:
>
>[...]
> I have done exactly what you want by doing the following, which is
> much like examples in K&R (1st edition) page 131 and page 140:
> 
> typedef struct node
> 	{
> 	...
> 	...
> 	node *next;
> 	} NODE, *PNODE;
> [...]

Smith probably means

 typedef struct node
	{
	...
	struct node *next;
	} NODE, *PNODE;

This works fine in MSC.

However, there is nothing wrong with

struct node
	{
	...
	struct node *next;
	};

(actually, K&R use several examples like this), so it surprises me that
MSC will not have it.

Heine

davidsen@sungod.crd.ge.com (ody) (08/19/89)

  I don't know what version of MSC you used, but I suspect that you have
a typo you just don't see. I tried it on versions 3, 4, and 5 and it
worked perfectly on all of them.

Here's the output for the one on a machine which can send stuff to UNIX
easily, I used the -Fs output to generate a source listing.



                                                                       PAGE   1
                                                                       08-18-89
                                                                       17:07:11

  Line#  Source Line                          Microsoft C Compiler Version 4.85

      1  #include <stdio.h>
      2  
      3  struct node {
      4    char name[80];
      5    int flags;
      6    struct node *next, *prev;
      7  } *head = NULL;

Global Symbols

Name                      Class   Type              Size   Offset

head. . . . . . . . . . . global   far pointer         4    0000

Code size = 0000 (0)
Data size = 0004 (4)
Bss size  = 0000 (0)

No errors detected
================ end include ================
	bill davidsen		(davidsen@crdos1.crd.GE.COM)
  {uunet | philabs}!crdgw1!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me

nts0302@dsacg3.UUCP (Bob Fisher) (08/21/89)

From article <680010@hpmwjaa.HP.COM>, by jeffa@hpmwtd.HP.COM (Jeff Aguilera):
#> struct node {
## 	
## 	...
## 	...
## 	struct node *next;
## 	};
## as it is with standard C, but this doesn't work with MS-C.
# 
# Are you SURE that doesn't work?  MS-C handles my linked lists
# perfectly.  
 
I doubt it.  struct node cannot include itself in its own definition.
Try swapping the last two lines like this.

	struct node {

	...
	...
	};
	struct node *next;

-- 
Bob Fisher (osu-cis!dsacg1!bfisher) 614-238-9071 (Autovon 850-9071)
From the Internet: bfisher%dsacg1.uucp@daitc.arpa
US Defense Logistics Agency Systems Automation Center
DSAC-TSX, Box 1605, Columbus, OH 43216-5002

chad@csd4.csd.uwm.edu (D. Chadwick Gibbons) (08/21/89)

In article <1575@dsacg3.UUCP> nts0302@dsacg3.UUCP (Bob Fisher) writes:
|From article <680010@hpmwjaa.HP.COM>, by jeffa@hpmwtd.HP.COM (Jeff Aguilera):
|#> struct node {
|## 	struct node *next;
|## 	};
|## as it is with standard C, but this doesn't work with MS-C.
|I doubt it.  struct node cannot include itself in its own definition.

	..but it can indeed contain a pointer to itself.  This is a valid C
construction, and is mentioned fairly early in the "Structures" chapter of
K&R.  Without this, data structures such as b-trees would be impossible.

struct _btree {
    char *datum;
    struct _btree *left, *right;
};
--
D. Chadwick Gibbons |  Internet: chad@csd4.csd.uwm.edu

gwyn@smoke.BRL.MIL (Doug Gwyn) (08/22/89)

In article <1575@dsacg3.UUCP> nts0302@dsacg3.UUCP (Bob Fisher) writes:
>I doubt it.  struct node cannot include itself in its own definition.

Recursive declarations are prohibited, but it is perfectly legitimate
to include a pointer to the same structure as one of its members.  The
structure tag is in scope, and since all structure pointers smell
alike, the compiler should have no difficulty in dealing with this
common case (which is much like examples used in K&R and other good
C programming textbooks).  The C Standard requires that this usage be
supported.

>Try swapping the last two lines like this.

Thereby producing something totally unusable for his purposes??

carlp@frigg.iscs.com (Carl Paukstis) (08/22/89)

In article <Aug.16.14.48.38.1989.11505@caip.rutgers.edu> shuang@caip.rutgers.edu (Shuang Chen) writes:
>I am trying to use struct to implement a linked list with Microsoft C.
>typedef struct {
>	...
>	node *next;
>	} node;
>
>Actually I tried to use
>
>struct node {
>	...
>	struct node *next;
>	};
>as it is with standard C, but this doesn't work with MS-C.

Chen doesn't mention which version of MSC - both of the above constructs
work just fine with v5.10, even in "W3" (bitchy) warning mode they
(properly) don't generate any messages whatsoever.

However, I can see how MS could have implemented a version which choked
on the first construct but compiled the second OK.  I'm certainly no
compiler guru, but it seems to me that the second example gives the
compiler some needed information sooner.

Would things improve if we changed the first example to start with
  typedef struct node {...
?

--
Carl Paukstis       "I'm the NRA"         |  DOMAIN:   carlp@iscuva.ISCS.COM   
    <political message goes here>         |  UUCP:     ...uunet!iscuva!carlp  
                                          |  GEnie: carlp         BIX: carlp
I speak for myself, not my employer.      |  Ma Bell:  +1 509 927 5439

john@frog.UUCP (John Woods) (08/22/89)

In article <680010@hpmwjaa.HP.COM>, jeffa@hpmwtd.HP.COM (Jeff Aguilera) writes:
> > struct node {
> > 	...
> > 	...
> > 	struct node *next;
> > 	};
> > as it is with standard C, but this doesn't work with MS-C.
> Are you SURE that doesn't work?  MS-C handles my linked lists
> perfectly.  

I'd suspect that the original person has forgotten to properly take note
of the MiS-C memory model qualifiers, "near", "far", and
"you_cant_get_there_from_here"...    ;-)
-- 
John Woods, Charles River Data Systems, Framingham MA 508-626-1101
...!decvax!frog!john, john@frog.UUCP, ...!mit-eddie!jfw, jfw@eddie.mit.edu

lmb@ibmpa.UUCP (Larry Breed) (08/24/89)

In article <1989Aug18.101819.3634@twwells.com> bill@twwells.com (T. William Wells) writes:
>
>struct node {
>	...
>};
>foo()
>{
>	struct node {
>		struct node *next;
>	};
>
>The "struct node *" is, I think, going to refer to the global struct
>node, not the local one; in that case, adding an additional "struct
>node;" at the start of the function will fix things.

The struct node defined in foo has scope starting at the left brace of
its declaration.  (pANS 3.1.2.1 paragraph 5.)  This was true in K&R C
as well.  Therefore the "struct node *" refers to the local struct node.

This odd rule for where scope begins is the reason for the curious
statement (3.1.2.1 para 4):

  "Two identifiers have the same scope if and only if the scopes
   terminate at the same point."

It has to be described this way because every identifier's scope
starts at a different point.

-- 
Larry Breed
inet: lmb%ibmsupt@uunet.uu.net
uucp: uunet!ibmsupt!lmb		(415) 855-4460