473,803 Members | 3,913 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 #1
15 2049
>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

"dutchgoldt ony" <du***********@ gmail.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.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

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

Similar topics

15
2783
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
5304
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
3415
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
2277
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
9681
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
2112
by: mohan | last post by:
Hi All, How to implement virtual concept in c. TIA Mohan
13
5024
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
2397
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
9564
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10316
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
9125
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
7604
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
6842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5500
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
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
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
3
2970
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.