4 Replies - 516 Views - Last Post: 26 May 2012 - 08:46 PM Rate Topic: -----

#1 CheckersW  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 198
  • Joined: 04-April 09

How should I order my structs/typedefs? (C)

Posted 25 May 2012 - 11:52 PM

The code:
typedef int item;
typedef node* Node;

typedef struct _node {
   item value;
   Node next;
} node;

typedef struct _stack  {
   Node first;
} stack;



The issue I'm having is that my struct "node" and my typedef "Node" are sort of interdependent, so if I define "node" first, the compiler freaks out (DynamicStack.c:13: error: expected specifier-qualifier-list before 'Node'), but if I define "Node" first, the compiler has a different freakout.

How should I order these definitions?

Is This A Good Question/Topic? 0
  • +

Replies To: How should I order my structs/typedefs? (C)

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 4891
  • View blog
  • Posts: 11,286
  • Joined: 16-October 07

Re: How should I order my structs/typedefs? (C)

Posted 26 May 2012 - 03:37 AM

Ack, don't typedef pointers! How the hell do you know it's a pointer then?

typedef int item;

typedef struct _node {
	item value;
	struct _node *next;
} node;

typedef struct {
	node *first;
} stack;



If you must, well, you still have to use the full syntax on the next. You then do the second typedef after it's declared e.g.
typedef struct _node {
	item value;
	struct _node *next;
} node;

typedef node *Node;


Was This Post Helpful? 2
  • +
  • -

#3 turboscrew  Icon User is offline

  • D.I.C Regular

Reputation: 69
  • View blog
  • Posts: 460
  • Joined: 03-April 12

Re: How should I order my structs/typedefs? (C)

Posted 26 May 2012 - 03:46 AM

Compiler needs to be tols about the existence of a symbol before it can be used.

You can't typedef a new type until all parts of the "old" type are at least declared.

That's why, if you define a self-referencing structure, you need to use the structure tag. The typedef is not defined yet when you are defining the self-referring field. The tag acts as a declaration.

like:

typedef struct my_struct
{
  struct my_struct * self_reference; /* my_type is not yet known */
} my_type;


Was This Post Helpful? 0
  • +
  • -

#4 CheckersW  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 198
  • Joined: 04-April 09

Re: How should I order my structs/typedefs? (C)

Posted 26 May 2012 - 07:19 PM

Excellent, thanks all.

View Postbaavgai, on 26 May 2012 - 04:37 AM, said:

Ack, don't typedef pointers! How the hell do you know it's a pointer then?


And yeah... I'm not a fan of typedeffing pointers, but it's part of my assignment and my lecturer requires that it be done like that (automated testing). Unfortunate, I know...
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1935
  • View blog
  • Posts: 5,759
  • Joined: 05-May 12

Re: How should I order my structs/typedefs? (C)

Posted 26 May 2012 - 08:46 PM

View Postbaavgai, on 26 May 2012 - 03:37 AM, said:

Ack, don't typedef pointers! How the hell do you know it's a pointer then?


This is where Hungarian notation pays huge dividends...

typedef struct _node
{
    // Other node fields here

    struct _node * m_pnode;    // 'm_pnode' translates to member pointer to node
} NODE;

typedef NODE * PNODE;          // 'PNode' translates to pointer to node

PNODE g_pnodeHead = 0;         // 'g_pnodeHead' translates to global pointer to node called 'Head'.

PNODE PnodeFindNth(int n)      // function returns a pointer to a node that was found to be the n-th element.



Personally, though, I agree with baavgai that one shouldn't typedef pointers. In Windows programming, though PCWSTR is so much easier to type and understand as a pointer to a constant wide character string as opposed to 'const wchar_t *' or some such. :-)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1