LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 08-10-2004, 10:34 AM   #1
LuderForChrist
Member
 
Registered: Jun 2004
Posts: 37

Rep: Reputation: 15
calling global structs


Im the progress still of porting C++ to C, yes yes yes I know, but ran across another problem.

I'm making each C++ class into a C struct,
and I need to know how to inherit another struct like one would inherit a class in C++.

Thanks guys! (and gals)
 
Old 08-10-2004, 10:42 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You can't meld structs together, but you can nest them:
Code:
struct base_struct
{
  int some_stuff;
  char *some_more_stuff;
};

struct lesser_struct
{
  int yet_more_stuff;
  struct base_struct base_stuff;
};
Is that what you were looking for?
 
Old 08-10-2004, 10:47 AM   #3
LuderForChrist
Member
 
Registered: Jun 2004
Posts: 37

Original Poster
Rep: Reputation: 15
Yea I believe that it is! Let me give it a try. thanks
 
Old 08-10-2004, 11:11 AM   #4
LuderForChrist
Member
 
Registered: Jun 2004
Posts: 37

Original Poster
Rep: Reputation: 15
mmmmm let me see if I got this

FILE 1
Code:
struct THEA A
{
     int blah ;
     int more crap;
};
FILE2
Code:
struct THEB B
{
    int look;
    struct THEA A;
}

............ later function the struct THEB B, was passed in

A->blah = the_result;
         OR
B->blah = the_result
neither work so ...... mmmmmmmmmmm what am I doing wrong

Last edited by LuderForChrist; 08-10-2004 at 11:16 AM.
 
Old 08-10-2004, 11:29 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You need to declare your structures like:
Code:
struct THEA
{
  int blah;
  int more_crap;
};

struct THEB
{
  int look;
  struct THEA A;
};

int main(void)
{
  struct THEB B;

  B.look = 1;
  B.A.blah = 2;
  B.A.more_crap = 86;

  return 0;
}
You can't use one of THEA's members directly using B. You have to step through the nesting like that. You could of course pass &B.A to a function then use A->blah or A->more_crap in that function. But you can't do something like B.my_crap in my example above. You have to do B.A.more_crap.
 
Old 08-10-2004, 11:43 AM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Here's a simple example. Say you're tracking book sales. Every book has a name and a number of pages and so forth. So you set up a book struct like so:
Code:
struct book_base
{
  char *name;
  int num_pages;
};
Now, normally you'd set up array of struct book_base, one element for each book you're tracking. So say you had 3 books. You might do something like:
Code:
{
  struct book_base books[3] =
  {
    { "Book 1", 213 }, { "Book 2", 99 }, { "Book 3", 792 }
  };
}
Now let's say you have 50 of each book and you want to track where they are. You could do something like this:
Code:
struct indiv_book
{
  int sold;              // 0 = not sold, still in store; 1 = sold
  time_t sell_date;//  0 if not yet sold otherwise time_t value of when sold
  struct book_base *book; // Pointer to book information
};
Then you might do something like:
Code:
{
  struct indiv_book book[150];  // 50 each of 3 books

  book[0].sold = 0;  // Not sold yet
  book[0].sell_date = 0; // Set to time(0) when sold
  book[0].book = &books[1]; // This particular book is a "Book 2".
}
Then you could see the name of book[0] that you're tracking by doing: printf("%s\n", book[0].book->name);

Or you could check how many pages book[0] has by looking at book[0].book->num_pages.

Clear as mud?

Last edited by itsme86; 08-10-2004 at 11:48 AM.
 
Old 08-10-2004, 12:40 PM   #7
LuderForChrist
Member
 
Registered: Jun 2004
Posts: 37

Original Poster
Rep: Reputation: 15
almost but Im trying to set a variable from the first struct not something from the second struct.
 
Old 08-10-2004, 12:55 PM   #8
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Then you would do like: book[0].book->name = "new name";

EDIT: Or you could do books[1].name = "new name"; or course if you wanted to change it directly without going through an indiv_book struct instance.

Last edited by itsme86; 08-10-2004 at 01:12 PM.
 
Old 08-11-2004, 12:42 AM   #9
bruce ford
Member
 
Registered: Jul 2004
Location: Munich, Germany
Distribution: Sun Solaris 8, SuSE 9.0
Posts: 43

Rep: Reputation: 15
hi!

ever tried something like this:

Code:
struct base_struct
{
  int some_stuff;
  char *some_more_stuff;
};

struct lesser_struct : public base_struct
{
  int yet_more_stuff;
};
(Thanks itsme86 for the example)
I have learned that in C++, structs are EXACTLY like classes except that the default visibility is public instead of private.
So, you also could have used classes instead.

So long...
bruce
 
  


Reply



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
copy structs in c alaios Programming 10 09-10-2005 02:31 PM
odd recursion: calling "by hand" vs calling by cronscript... prx Programming 4 02-12-2005 04:59 PM
pointers to structs in C spuzzzzzzz Programming 5 06-03-2004 05:41 PM
static structs? simbo Programming 3 02-05-2004 04:00 AM
Self referential structs in C? MadCactus Programming 14 01-28-2004 05:29 PM

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

All times are GMT -5. The time now is 06:36 AM.

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