LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-07-2012, 02:59 AM   #1
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Rep: Reputation: Disabled
can a structure in C,refer other structure??


The scenario is like this..

struct server2
{
int agent ;
struct server2 *next;
};

struct server1
{
int agent;
struct server1 *next;
};

while going to update server1 linked list
server2 linked list gets updated automatically..

please add your important method & comments
 
Old 12-07-2012, 03:42 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,522

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Please rephrase your question; I don't quite understand what you are asking.

The minimal code you presented only shows declarations of two distinct structures which are defined similarly (redundant, IMO), but you have no instances showing how these structures will be used.

The compiler will see struct server2 and struct server1 as two distinct data types.

Last edited by dwhitney67; 12-07-2012 at 03:43 AM.
 
Old 12-07-2012, 03:55 AM   #3
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Original Poster
Rep: Reputation: Disabled
that's the thing i am asking..
as i declare two distinct structure,but i need that the member of "server1" should refer "server2"
that is any modification in "server1->agent" should reflect in "server2->agent" as well
 
Old 12-07-2012, 04:10 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,522

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by yahoosam View Post
that's the thing i am asking..
as i declare two distinct structure,but i need that the member of "server1" should refer "server2"
that is any modification in "server1->agent" should reflect in "server2->agent" as well
This is analogous to having two variables, of type int, called 'i' and 'j' and wishing for both to have the same value. The only way I know to accomplish that is two either set these variable to the same value, or set the first to a value, then the second equal to the first. Do you get what I'm driving at?
Code:
int i, j;

i = 5;
j = 5;

/* or *

i = 5;
j = i;
The same is going to apply to your structures. If you declare a variable of type struct server1 and start populating it, you will need to do something similar with the variable of struct server2.

If you are going to have basically identical structures, then why not just declare one type of structure, and then have two variables of that same type? For example:
Code:
struct Server
{
    int agent;
    struct Server* next;
};

struct Server  srv1;
struct Server* srv2 = &srv1;
With the code above, each time srv1 is manipulated, srv2 will point to the same region.
 
Old 12-07-2012, 07:33 AM   #5
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Original Poster
Rep: Reputation: Disabled
suppose the case when
Code:
int *p=78;
int i=&p;
so,actually i have to declare two different structures..but any change in first structure's member will reflect in other.
even if any node is deleted.
as same the above code related,any change in p would reflect in variable i.

so i am asking in structure case will it be possibly happen.
please add the way with your important comments.
Thanks
 
Old 12-07-2012, 07:50 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally Posted by yahoosam View Post
suppose the case when
Code:
int *p=78;
int i=&p;
Actually, I don't think this will do what you think it will do. Maybe you meant
Code:
int i=78;
int *p=&i;
Also, could you please explain what are you trying to achieve in the first place? Why do you need two distinct structures? And by "two distinct structures", do you mean "two different data types" or "two instances of the same type"? Do you only need to refer to the same data by different identifiers or do you need the data to be duplicated as well?
 
Old 12-07-2012, 07:51 AM   #7
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,070

Rep: Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106
You need some kind of connection between the objects.
In C, you would need to manage the connection yourself in every place within the code that affects the connection.
That is one advantage of of C++. In C++ you can keep the management of the connection internal to the implementation of those two structs.

Quote:
Originally Posted by yahoosam View Post
any change in first structure's member will reflect in other.
For that, one could have and always use a pointer to the other's data instead of having its own data.

Quote:
even if any node is deleted.
But that is more complicated. That sounds like it requires each struct to have a pointer to the other and operations on each explicitly extend to the other.

At that point, it seems silly to even have two structures. Maybe you really want one struct that is a member of two different linked lists.

Last edited by johnsfine; 12-07-2012 at 07:55 AM.
 
Old 12-07-2012, 07:51 AM   #8
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,522

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by yahoosam View Post
so,actually i have to declare two different structures..but any change in first structure's member will reflect in other.
even if any node is deleted.
As I indicated earlier, this is not possible. Two variables, referencing different objects (ie types) will be located in your application's memory in different locations.

You need to either a) accept this limitation and realize that you will need to perform two operations to keep your lists in sync, or b) revisit your application's design to figure why is it that you need two different structures that have the same data.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
structure inside a structure in C batman4 Programming 3 09-13-2012 05:52 AM
Convert directory structure from long file names in Linux to DOS 8.3 structure? manorina Linux - Software 5 09-12-2009 09:18 AM
Home Jail Folder Structure like Gobolinux Directory Structure luispt Linux - General 3 07-26-2008 06:46 PM
structure within a structure in C knobby67 Programming 3 03-06-2007 09:00 AM
structure os2 Programming 6 05-29-2005 07:39 PM


All times are GMT -5. The time now is 09:11 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration