LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 07-25-2006, 02:38 PM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
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.
 
Old 07-25-2006, 03:35 PM   #2
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
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!
 
Old 07-25-2006, 03:52 PM   #3
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
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.
 
Old 07-25-2006, 03:57 PM   #4
burninGpi
Member
 
Registered: Mar 2006
Location: Fort McMurray, Canada
Distribution: Gentoo ~amd64
Posts: 163

Rep: Reputation: 30
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.
 
Old 07-25-2006, 04:44 PM   #5
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
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!
 
Old 07-28-2006, 02:22 PM   #6
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
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.
 
  


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
Variable available scope problem ArthurHuang Programming 1 05-22-2006 01:16 AM
how the scope of RHCE ? tofee Linux - Newbie 1 04-10-2006 07:44 AM
scope of an object and global scope lucky6969b Programming 7 12-09-2005 10:09 PM
java variable scope - use of "this" keyword zeppelin147 Programming 1 11-21-2005 11:04 PM
scope suchi_s Programming 1 10-30-2004 08:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:49 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration