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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
07-25-2006, 02:38 PM
|
#1
|
Senior Member
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109
Rep:
|
Variable scope
I have a situation where I have three structures defined and I will (depending on a value) use one of them. I could use if but declaring it within the if will make it vanish after the if is finnished. Here's how it looks:
Code:
struct one {
...
}
struct two {
...
}
struct three {
...
}
int main() {
...
if (value==0)
struct one dg;
else if (value==1)
struct two dg;
else
struct three dg;
...
and here I need to use dg, but it's out of scope.
}
Any ideas? I could do it the ugly way and declare three dgs and use one of them but I was hoping that I could declare only one instance somehow.
|
|
|
07-25-2006, 03:35 PM
|
#2
|
Member
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173
Rep:
|
Hi,
I think it's not very clear what you want to do. For what I understand you want to allocate one of the three structures, but then use them all in the same way. I noticed you use the word "instance" which is very OO, and C is very very very structured.
Strictly speaking, if you define a variable inside an if block, then it will exist only there. And if you define it above the ifs, then you will only have one type.
I am quite sure that you cant make polymorphism in C.
Maybe if you explain a little more what you want to do, we can come up with some solution.
Hope this is useful.
Cheers!
|
|
|
07-25-2006, 03:52 PM
|
#3
|
Senior Member
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109
Original Poster
Rep:
|
The word 'instance' was used because I couldn't think of a better word in english. So that's just poor english, not a secret love for OO. :P
I'll try to explain a little better. I have three structures describing a datagram where one member differ, one is using tcphdr, one udphdr and one using icmphdr. The program can be run using a --proto flag specifying the protocol (default icmp).
So now I want to create something like this:
struct tcpdg/udpdg/icmpdg dg;
I then only have one "instance": dg. Not three. I can then keep using ifs to operate differently on dg when necessary. I does not need to check s.protocol whenever I want to operate on all the other parts of the structure, which is the same for all three kinds. Having only one name: dg, to care about would save me a lot of annoyance. Instead of having something like tdg, udg and idg and then use ifs whenever I need to use it. I hope you get the point here.
I tried this:
struct s.protocol==0?tcpdg:s.protocol==17?udpdg:icmpdg dg;
which appearantly did not work. I was just hoping there would be some way I could skip having to declare three "instances" (you give me a better word and I'll use it) which, to me, looks ugly since only one will actually be used. Plus I need to use a if to know which name I should use (idg, udg or tdg).
EDIT:
btw, the structures are all the same size: 4096 bytes.
Last edited by Ephracis; 07-25-2006 at 04:04 PM.
|
|
|
07-25-2006, 03:57 PM
|
#4
|
Member
Registered: Mar 2006
Location: Fort McMurray, Canada
Distribution: Gentoo ~amd64
Posts: 163
Rep:
|
in C++, you could do:
Code:
class DG {
}
class One: public DG {
...
}
class Two: public DG {
...
}
class Three: public DG {
...
}
int main(){
...
DG *dg;
switch (value){
case 0: dg = new One(); break;
case 1: dg = new Two(); break;
default: dg = new Three(); break;
}
*HERE*
and now the dg variable stays, and you only have one type.
The dreaded goto might be the only way to do it in C.
Code:
struct one {
...
}
struct two {
...
}
struct three {
...
}
int main() {
...
if (value==0)
goto ONE;
else if (value==1)
goto TWO;
else
goto THREE;
ONE:
struct one dg; goto END;
TWO:
struct two dg; goto END;
THREE:
struct three dg ; goto END;
END:
...
}
I'm not sure if it will work, but try it anyway.
|
|
|
07-25-2006, 04:44 PM
|
#5
|
Member
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173
Rep:
|
Hi again,
in C you could use a UNION. For instance:
Code:
struct PacketHeader {
int protocolType;
// other common fields
union {
struct tcphdr tcphdr;
struct udphdr udphdr;
struct icmphdr icmphdr;
} ProtocolSpecificData;
};
then you can use the struct normally:
Code:
struct PacketHeader myHeader;
just be careful to load only one field of the attributes of the union, since they will use the same memory space.
The other solution I can think of depends heavily in how different are this parts you are trying to store. Maybe, since they are all of the same size, you can have the different parts as an array of chars (since you are dealing with protocol headers it MIGHT make sense but I dont know for sure because I dont know what you want to save).
And as the word instance, its not that bad, but this modern times makes you frighten jajajaja
But I think the proper word for that is just variable
I learnt all the union stuff from the Kernighan & Ritch book (The C programming Language) which is a book I highly recommend you if you want to program in C (its not for beginner but you dont look like one).
If something about the union was not very clear, shoot again
Hope this is useful.
Cheers!
|
|
|
07-28-2006, 02:22 PM
|
#6
|
Senior Member
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109
Original Poster
Rep:
|
Thank you. Union was something really useful that I've seem to have missed. It did the trick perfectly.
I use an old school-book for C++ but currently Internet gives me much better help at solving problems. I will probably buy a "real" c book soon, thou. I will keep that book in mind.
Again, thank you. I've seen union a lot in the tcp/ip include files but never understood exactly what it was, until now.
|
|
|
All times are GMT -5. The time now is 12:49 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|