473,834 Members | 2,362 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
15 2050
"dutchgoldt ony" <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_Keit h) 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 niardgoverdgovn rdgog.
Nov 16 '05 #15

"Michael Wojcik" <mw*****@newsgu y.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
2788
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 as member variables. So including a forward declaration doesnt help, does it? Faced with these, I had the following options: -Include the appropriate header in the header file that contains the class definition that has a member variable that is...
138
5311
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 array(i.e)it will have the address of the first element.In the the program below am not able to increment the value stored in x,which is the address of the first element.Why am I not able to do that?Afterall 1 is also a hexadecimal number then...
10
3418
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 <offset>). What I would like to do is create an API something like this: #include <stddef.h> struct MemMap { unsigned char apple; // 8 bits on my platform
2
2279
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 means to do this using Managed VC++. It is very simple in unmanaged VC++, but I have no idea how to do this in MVC++....thanks! -TGF
13
9684
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
2113
by: mohan | last post by:
Hi All, How to implement virtual concept in c. TIA Mohan
13
5025
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
2398
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 that I use to communicate to some unmanaged code in a DLL written in C. It is not complicated, but will change and I would like to be able to sequentially access it without explicitly referring to each and every element. Here is the structure:
11
2294
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include <iostream> using namespace std;
0
9796
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10503
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9326
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7754
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5624
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5790
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3079
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.