🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.sys.acorn.tech
7 messages
7 total messages Started by Philip R. Banks Thu, 15 Oct 1992 10:10
C query.
#330
Author: Philip R. Banks
Date: Thu, 15 Oct 1992 10:10
53 lines
2321 bytes
   Okay here is a little wee problem I am hoping some C type can answer for
me. Quickly please, before I take to this compiler with a blunt
instrument... :)

The problems is thus I have defined a structure like so :-

typedef struct {
   char *name;
   char *comment;
   struct filenode *next;
   struct filenode *prev;
   struct filenode *parent;
   struct filenode *sublist;
   int load;
   int exec;
   int length;
   char flag;
} filenode;

In the same file inside a procedure called AddNode I do the following :-

filenode *AddNode(char *name, filenode *parent, int type, char *comment) {
/* Adds a node to the sub list of the node passed as the parent. */
    filenode *newnode, *sublist;
    int namelength,commentlength;
    char *charblock1,*charblock2;
...
    if (sublist != NULL) {
       filenode *scratch;
       int flag;
...
             if ((*scratch).next != NULL) scratch->next->prev = (struct filenode *) newnode;

With the last line being the problem one. The compiler gives me the serious
error number 140 " 'struct filenode' not yet defined - cannot be selected
from ". Now given that the structure is defined in the *same* file I find
this a little hard to believe. Also the if statement is in the same scope as
the declaration of the variable scratch.

   So can anyone answer what I am doing wrong that the Norcroft compiler is
soooo helpfully not telling me? 

   I knew there was a good reason for sticking to Assembler. <mumble)
<mumble).

    Philip

--
Philip R. Banks  Syntax: mail < banks_p@kosmos.wcc.govt.nz >            @@@@@/|
                                                                        @@@@/#|
                                                                        @@@/##|
And who are we to criticize or scorn the the things that they do?       @@/---|
For we shall seek and we shall find Ammonia Avenue. ---A.P.P.           @/    |
Re: C query.
#334
Author: M.J. Williams
Date: Thu, 15 Oct 1992 14:43
36 lines
1571 bytes
In article <2qdtw7j025n@khantazi.welly.gen.nz> banks_p@kosmos.wcc.govt.nz writes:
>The problems is thus I have defined a structure like so :-
>
>typedef struct {
>   char *name;
>   char *comment;
>   struct filenode *next;
>   struct filenode *prev;
>   struct filenode *parent;
>   struct filenode *sublist;
>   int load;
>   int exec;
>   int length;
>   char flag;
>} filenode;

This defines an anonymous structure, and assigns it to the typedef filenode.
You want something like:

typedef struct filenode {
   char *name;
   char *comment;
   struct filenode *next;
   struct filenode *prev;
   struct filenode *parent;
   struct filenode *sublist;
   [...]
} filenode;

Hope this helps,
Mike
--
_______________________________________________________________________________
 \   ^   / Michael Williams                     Part II Computer Science Tripos
  |\/|\/\  MJW19@phx.cam.ac.uk                          University of Cambridge
  |  |(__) Cymdeithas Genedlaethol Traddodiad Troi Teigrod Mwythus Ben I Waered
Re: C query.
#336
Author: M.J. Williams
Date: Thu, 15 Oct 1992 14:55
21 lines
1400 bytes
In article <1992Oct15.144310.3530@infodev.cam.ac.uk> I wrote:
>This defines an anonymous structure, and assigns it to the typedef filenode.
>You want something like:
>
>typedef struct filenode {
>   char *name;
>   char *comment;
>   struct filenode *next;
>   struct filenode *prev;
>   struct filenode *parent;
>   struct filenode *sublist;
>   [...]
>} filenode;

Oh, and use (filenode *) instead of (struct filenode *) for tidier programming.
Or, of course, leave out the typedef all together...
--
_______________________________________________________________________________
 \   ^   / Michael Williams                     Part II Computer Science Tripos
  |\/|\/\  MJW19@phx.cam.ac.uk                          University of Cambridge
  |  |(__) Cymdeithas Genedlaethol Traddodiad Troi Teigrod Mwythus Ben I Waered
Re: C query.
#338
Author: Ug!
Date: Thu, 15 Oct 1992 20:49
69 lines
2626 bytes
Philip R. Banks writes:
>    Okay here is a little wee problem I am hoping some C type can answer for
> me. Quickly please, before I take to this compiler with a blunt
> instrument... :)
> 
> The problems is thus I have defined a structure like so :-
> 
> typedef struct {
>    char *name;
>    char *comment;
>    struct filenode *next;
>    struct filenode *prev;
>    struct filenode *parent;
>    struct filenode *sublist;
>    int load;
>    int exec;
>    int length;
>    char flag;
> } filenode;

Hmm.  Well, I can see problems already!  The type name 'filenode' isn't defined
within the structure definition!  It's not defined until the end of the typedef
has been reached.

Try this instead:

    struct _filenode
      {
        char             *name,
                         *comment;
        struct _filenode *next,
                         *prev,
                         *parent,
                         *sublist;
        int              load,
                         exec,
                         length;
        char             flag;
      };

    typedef struct _filenode filenode, *filenodeptr;

This will define the types correctly (I also tend to define a pointer type:
in this case, 'filenodeptr').

> [more code deleted]
> 
> With the last line being the problem one. The compiler gives me the serious
> error number 140 " 'struct filenode' not yet defined - cannot be selected
> from ". Now given that the structure is defined in the *same* file I find
> this a little hard to believe. Also the if statement is in the same scope as
> the declaration of the variable scratch.

See above.

>    So can anyone answer what I am doing wrong that the Norcroft compiler is
> soooo helpfully not telling me? 

Not the Norcroft compiler, I'm afraid.  You would have similar problems with
*any* ANSI C compiler.  You simply *cannot* reference a typedef name until
*after* it is defined: this means that referencing the name *during* the
definition will not work.

>    I knew there was a good reason for sticking to Assembler. <mumble)
> <mumble).

...or learning the language (whatever it may be) that you are using... ;-}

								Ug!
Re: C query.
#340
Author: TMOTA
Date: Thu, 15 Oct 1992 22:05
40 lines
1757 bytes
banksie@khantazi.welly.gen.nz (Philip R. Banks) writes:

>The problems is thus I have defined a structure like so :-

>typedef struct {
>...
>   struct filenode *next;
>   struct filenode *prev;
>   struct filenode *parent;
>   struct filenode *sublist;
>...
>} filenode;

>...
>if ...scratch->next->prev = (struct filenode *) newnode;


I think your problem lies with your use of the word 'struct':

Probably, changing the cast to (filenode *) will be all that is necessary.
This is because now that you have defined 'filenode' as a TYPE you don't
need to use the 'struct' to identify it as a struct.

This may still cause problems (as 'filenode *' may be regarded as a different
type from 'struct filenode *' and thus give an implicit cast error/warning)

For such recursive definitions, I typically use something like:

typedef struct _bob
{
  _bob *thingo;    /* or possibly 'struct _bob *' ... my memory is dim */
} bob;

... it defines a struct type _bob that you can use in the declaration but
afterwards you use 'bob' everywhere (i.e. bob *mypointer). This should
work fine.
-- 
_________________  "I'd like to answer this question in two ways:
  /____ _ _/_ __       First in my normal voice, and then
 // / //_//_ /_/       in a silly, high-pitched whine." (Monty Python)
Re: C query.
#363
Author: Andy Duplain
Date: Fri, 16 Oct 1992 13:50
44 lines
2009 bytes
In article <1992Oct16.094939.11437@waikato.ac.nz> banksie@khantazi.welly.gen.nz writes:
>Philip R. Banks writes:
>>    Okay here is a little wee problem I am hoping some C type can answer for
>> me. Quickly please, before I take to this compiler with a blunt
>> instrument... :)
>> 
>> The problems is thus I have defined a structure like so :-
>> 
>> typedef struct {
>>    char *name;
>>    char *comment;
>>    struct filenode *next;
>>    struct filenode *prev;
>>    struct filenode *parent;
>>    struct filenode *sublist;
>>    int load;
>>    int exec;
>>    int length;
>>    char flag;
>> } filenode;
>
>Hmm.  Well, I can see problems already!  The type name 'filenode' isn't defined
>within the structure definition!  It's not defined until the end of the typedef
>has been reached.

	Well he isn't referencing "type name filenode" from within the
	structure definition.  The compiler will not complain at this
	stage because it knows how big the pointer to structure is, which
	is the same as pointer to anything, i.e. 1 word.  Philip is getting
	confused between structs and typedef names and this can be made
	more obvious by doing:
	
		struct filenode {
		
			blah... blah... blah
		
		};
		
		typedef struct filenode *FileNode;
	

-- 
Andy Duplain, BT Customer Systems, Brighton, UK.            duplain@rtf.bt.co.uk
#define	DISCLAIMER      My views and opinions are my own, and not my company's
Re: C query.
#365
Author: Geoff Riley
Date: Fri, 16 Oct 1992 21:05
33 lines
1431 bytes
In article <2qdtw7j025n@khantazi.welly.gen.nz> banks_p@kosmos.wcc.govt.nz writes:
>   Okay here is a little wee problem I am hoping some C type can answer for
>me. Quickly please, before I take to this compiler with a blunt
>instrument... :)

Not too difficult to explain.

I shall give a different example:

typedef struct fred {
	int an_int ;
	struct fred * next ;
	struct fred * prev ;
} fred ;

That is a declaration of two things: a struct called fred, which may be used
as:
	struct fred a_fred_variable ;
and a new type called fred, which may be used as:
	fred another_fred_variable ;

You would normally name the struct slightly differently in order to be able
to spot the difference easier.

Hope that helps.

I prefer assembler anyway ;-)
-- 
Cheerio,
        |~~~      (uucp address: arcturus@spuddy.uucp)
        |__|eoff  (Fidonet address: 2:250/208)

 * Meeeow ! Call Spuddy on (0203) 638780/638693 for FREE mail & Usenet access *
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads