ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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
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?
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.