473,386 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Using an instance of a struct as a member of that struct

Hi all,

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?

Thanks in advance for any help,
Cheers, Tony

Nov 15 '05 #1
15 2017
>I was just wondering if this is possible.

No, you cannot have an instance of a struct as a member of that
struct. Assuming that the struct has at least one other member,
that would require infinite memory.

You can, however, have a *pointer* to the struct as a member of
that struct. This is common with linked lists.
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
The above should be a struct _node *, not a node *.
You haven't defined the typedef 'node' yet.
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?


No. Not in context.

Gordon L. Burditt
Nov 15 '05 #2

"dutchgoldtony" <du***********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi all,

Re: Using an instance of a struct as a member of the struct
I was just wondering if this is possible.
Of course not, and with a bit of thought, you should realize why.
But that's not what you're trying to do in your code below.
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?


What did your compiler say? Anyway, yes it's legal.
The member 'connectedNodes' is not an array of
type 'struct _node' objects, but an array of type
'struct _node *' (pointer to 'struct _node') objects.

Reminder:
When you populate that array with pointers, be sure
it contains addresses of valid objects (i.e. you must
either allocate or define those objects somewhere first,
and if you allocate them, don't forget to free them
when you're done with them).

-Mike
Nov 15 '05 #3
On 2005-11-14, Gordon Burditt <go***********@burditt.org> wrote:
I was just wondering if this is possible.
No, you cannot have an instance of a struct as a member of that
struct. Assuming that the struct has at least one other member,
that would require infinite memory.

You can, however, have a *pointer* to the struct as a member of
that struct. This is common with linked lists.
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


The above should be a struct _node *, not a node *.
You haven't defined the typedef 'node' yet.
no, it shouldn't be struct _node *
it should be struct node *.

typedef struct node {
...
struct node *connectedNodes[2];
...
} node;
No. Not in context.

Gordon L. Burditt

Nov 15 '05 #4
Jordan Abel wrote:
On 2005-11-14, Gordon Burditt <go***********@burditt.org> wrote:


The above should be a struct _node *, not a node *.
You haven't defined the typedef 'node' yet.


no, it shouldn't be struct _node *
it should be struct node *.


Depends on your definition of "should". What Gordon had was perfectly
legal. Some people object to having a tag name different from the
resulting typedef, but others find it promotes clarity. Some dislike
typedefs for structs, period.

Brian
Nov 15 '05 #5
>> > The above should be a struct _node *, not a node *.
> You haven't defined the typedef 'node' yet.
no, it shouldn't be struct _node *
it should be struct node *.


Depends on your definition of "should".


I believe the point being brought up here is namespace issues.
What Gordon had was perfectly
I am not the original poster.
legal.
No, I don't believe it was. You can't use a typedef before
it's defined, and it's not defined until the *END* of the
definition.

typedef struct foo {
...
foo *x;
...
} foo;
isn't going to work. If you replace "foo *x;" with "struct foo *x;",
it will.
Some people object to having a tag name different from the
resulting typedef, but others find it promotes clarity. Some dislike
typedefs for structs, period.


Gordon L. Burditt
Nov 15 '05 #6
Gordon Burditt wrote:
> The above should be a struct _node *, not a node *.
> You haven't defined the typedef 'node' yet.

no, it shouldn't be struct _node *
it should be struct node *.
Depends on your definition of "should".


I believe the point being brought up here is namespace issues.


No, I don't think so.
What Gordon had was perfectly


I am not the original poster.


I know, you posted the original correct which changed the interior
declaration to struct _node *.
legal.


No, I don't believe it was. You can't use a typedef before
it's defined, and it's not defined until the END of the
definition.


Please reread the thread, and Jordan's comments.
typedef struct foo {
...
foo *x;
...
} foo;
isn't going to work. If you replace "foo *x;" with "struct foo *x;",
it will.


Who said it would?

Like I said, reread the thread, you've misinterpreted my remarks
entirely.

Brian
Nov 15 '05 #7
On 2005-11-14, Default User <de***********@yahoo.com> wrote:
Jordan Abel wrote:
On 2005-11-14, Gordon Burditt <go***********@burditt.org> wrote:


> The above should be a struct _node *, not a node *.
> You haven't defined the typedef 'node' yet.


no, it shouldn't be struct _node *
it should be struct node *.


Depends on your definition of "should". What Gordon had was perfectly
legal. Some people object to having a tag name different from the
resulting typedef, but others find it promotes clarity. Some dislike
typedefs for structs, period.


His use of the identifier "_node" is not legal.
Nov 15 '05 #8
Jordan Abel wrote:
On 2005-11-14, Default User <de***********@yahoo.com> wrote:

Depends on your definition of "should". What Gordon had was
perfectly legal. Some people object to having a tag name different
from the resulting typedef, but others find it promotes clarity.
Some dislike typedefs for structs, period.


His use of the identifier "_node" is not legal.


Oh, right. That's reserved in tag name space as well as file scope.

Brian
Nov 15 '05 #9
On 2005-11-15, Default User <de***********@yahoo.com> wrote:
Jordan Abel wrote:
On 2005-11-14, Default User <de***********@yahoo.com> wrote:

> Depends on your definition of "should". What Gordon had was
> perfectly legal. Some people object to having a tag name different
> from the resulting typedef, but others find it promotes clarity.
> Some dislike typedefs for structs, period.


His use of the identifier "_node" is not legal.


Oh, right. That's reserved in tag name space as well as file scope.


I thought it was reserved everywhere full stop. Because even block scope
names could collide with an internal name used in a macro expansion.
Nov 15 '05 #10
"dutchgoldtony" <du***********@gmail.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #11
Jordan Abel wrote:
On 2005-11-15, Default User <de***********@yahoo.com> wrote:
Jordan Abel wrote:
On 2005-11-14, Default User <de***********@yahoo.com> wrote:

> Depends on your definition of "should". What Gordon had was
> perfectly legal. Some people object to having a tag name different >> > from the resulting typedef, but others find it
promotes clarity. >> > Some dislike typedefs for structs, period.
His use of the identifier "_node" is not legal.


Oh, right. That's reserved in tag name space as well as file scope.


I thought it was reserved everywhere full stop. Because even block
scope names could collide with an internal name used in a macro
expansion.


No, double underscore or underscore followed by an uppercase letter are
reserved in all contexts. You could use _node as a struct member name
without causing a problem. I had to look up the rules (as usual).
That's why it often recommended just not to use a leading underscore so
there's error.

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Nov 15 '05 #12
Thanks for clearing that up, and for the posting pointers! That should
give me a bit to go on.

Cheers for the help,
Tony

Nov 15 '05 #13
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.

If I am trying to refer to that member of the struct:
"> struct node *connectedNodes[2];"
where I would have previously (albeit wrongly) have said, for example:
"n[i][j].connectedNodes[0] = &(n[0][j+1])"
should I now say:
"struct n[i][j].connectedNodes[0] = &(n[0][j+1])"
Do I have to add anything extra for the fact that connected nodes is a
struct within a struct or will this do.

Thanks again,
Tony

Nov 15 '05 #14

In article <3t************@individual.net>, "Default User" <de***********@yahoo.com> writes:
Jordan Abel wrote:
On 2005-11-15, Default User <de***********@yahoo.com> wrote:
Jordan Abel wrote:
>
> His use of the identifier "_node" is not legal.

Oh, right. That's reserved in tag name space as well as file scope.
I thought it was reserved everywhere full stop.
Not quite. 7.1.3 in C90 or C99:

- All identifiers that begin with an underscore and either an
uppercase letter or another underscore are always reserved
for any use.

- All identifiers that begin with an underscore are always
reserved for use as identifiers with file scope in both
the ordinary and tag name spaces.

So "_Node" is reserved anywhere, but "_node" isn't if it's not at
file scope. (That includes the tag name space, which is always at
file scope.)

Since implementations aren't required to distinguish case among
identifiers with external linkage (C90 6.1.2), it could be argued
that even at block scope, an identifier with a leading underscore and
lowercase name with external linkage is reserved, or should be.
Because even block
scope names could collide with an internal name used in a macro
expansion.


They could, but the standard doesn't reserve single-underscore-and-
lowercase-letter identifiers for that. If the implementation wants
to use reserved identifiers in the text of a macro, it should use
identifiers beginning with two underscores, presumably.
No, double underscore or underscore followed by an uppercase letter are
reserved in all contexts. You could use _node as a struct member name
without causing a problem. I had to look up the rules (as usual).
That's why it often recommended just not to use a leading underscore so
there's error.


(That's "no chance for error", presumably.) I agree, though some
seem to like to use identifiers with a leading underscore and
lowercase letter in macro replacement text (for user-written macros,
not ones provided by the implementation). That's legal as long as
the macro isn't used to generate a tag name or a file-scope
identifier.

--
Michael Wojcik mi************@microfocus.com

He smiled and let his gaze fall to hers, so that her cheek began to
glow. Ecstatically she waited until his mouth slowly neared her own.
She knew only one thing: rdoeniadtrgove niardgoverdgovnrdgog.
Nov 16 '05 #15

"Michael Wojcik" <mw*****@newsguy.com> wrote in message
news:dl********@news3.newsguy.com...
(That's "no chance for error", presumably.) I agree, though some
seem to like to use identifiers with a leading underscore and
lowercase letter in macro replacement text (for user-written macros,
not ones provided by the implementation). That's legal as long as
the macro isn't used to generate a tag name or a file-scope
identifier.


All these 'complicated' rules and their implications just
underscore :-) the validity of the 'best practice' advice:
"Leading underscores: Just Say No."

-Mike
Nov 16 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
10
by: Mark A. Odell | last post by:
Is there a way to obtain the size of a struct element based only upon its offset within the struct? I seem unable to figure out a way to do this (short of comparing every element's offset with...
2
by: TGF | last post by:
Hello, I have two classes....Class A and Class B. I also have a Struct C. Now I want to have 1 instance of Struct C accesible to both Class A and class B. Does anyone know the most efficient...
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
12
by: mohan | last post by:
Hi All, How to implement virtual concept in c. TIA Mohan
13
by: Kantha | last post by:
Hi all, I have declared an Union as follows typedef union { struct interrupt_bits { unsigned char c_int_hs_fs_status : 1, c_setup_intflag : 1,
9
by: Bill Grigg | last post by:
All, Can anyone supply an example or reference to an example of using reflection to determine the data types and array lengths contained in a nested stucture in C#? Actually, it is a structure...
11
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.