dutchgoldtony said:
I was just wondering if this is possible. I'm trying to implement a
viterbi decoder in C and am creating an array of nodes (the struct),
and an array of pointers to nodes (the member I'm worried about)
connecting to it like so:
// snippet start
typedef struct _node{
char state[2]; // The state of each node ("00","01","10","11")
int status ; // 1: Node is on potential path (exists), 0: not
node* connectedNodes[2]; // Array of ptrs to nodes connected to
this node
int edgeCost[2]; // Cost of edges coming from nodes
int cost; // Cost associated with node:
} node;
node n[4][maxDepth];
//snippet end
Is this line "node* connectedNodes[2];" legal?
There's been some confusion about who said (or meant) what, so I'll
summarize.
You shouldn't declare identifiers starting with "_"; many of them are
reserved for use by the implementation. There are some rules that
depend on what the following character is, but there's little point in
memorizing the details (I haven't); just avoid leading underscores
altogether, and you'll be fine.
Using "//" comments in Usenet postings is not recommended. They're
legal in C99 (and a common extension in pre-C99 compilers), but
wraparound can cause the end of a comment to appear on a line by
itself, making the code illegal. The older (and still supported)
"/* ... */" comments don't have this problem. In your case, the lines
haven't wrapped (at least in my news client), but they're overly long;
please limit your text to about 72 columns.
You can't have an instance of a struct as a member of that struct (the
resulting struct would be infinite in size), but you can have a
pointer to a struct as a member of the struct, which is what you're
trying to do here.
It's common to declare both a struct tag and a typedef for the same
type, and it's perfectly legal to use the same identifier for both.
But the typedef name can't be used before its declaration, which comes
at the end of the struct declaration.
Here's your declaration with the layout cleaned up and the identifier
"_node" changed to "node":
typedef struct node {
char state[2];
int status;
node *connectedNodes[2]; /* illegal, "node" is undeclared */
int edgeCost[2];
int cost;
} node;
The name "node" doesn't exist yet when you try to use it. You can either
use the struct tag:
typedef struct node {
char state[2];
int status;
struct node *connectedNodes[2];
int edgeCost[2];
int cost;
} node;
or you can declare the typedef using an incomplete type:
typedef struct node node;
struct node {
char state[2];
int status;
node *connectedNodes[2];
int edgeCost[2];
int cost;
};
But the best solution, IMHO, is to drop the typedef altogether:
struct node {
char state[2];
int status;
struct node *connectedNodes[2];
int edgeCost[2];
int cost;
};
and use the full name "struct node" whenever you want to refer to this
type. Hiding the full name behind a typedef does two things: it saves
you a few keystrokes (which is *not* much of a benefit), and it hides
the fact that the type is a structure. The latter can be useful if
you're creating some kind of abstract data type, but that doesn't
appear to be the case here; if the code that uses this type is going
to refer to its members, it has to know that it's a struct anyway, and
hiding that fact just obfuscates the code.