Pass Struct By Reference Or Value

Aug 20, 2009 at 9:39pm
I have declared and defined a struct in a header file, i.e.,...

1
2
3
4
5
6
7
8
9
10
11
12
13
struct MYSTRUCT {
   char* szChar;
   HFONT hFont;
:

MYSTRUCT ms[]= {
   "aaa", hCourNew;
   "bbb", hCourNew;
   "ccc", hCourNew;
   "ddd", hArial;
   "ddd", hArial;
...etc.,
};


Now I actually load the fonts in WM_CREATE, so that the font assignments in the structure don't actually work because the structure assignment occurs BEFORE the fonts are loaded in WM_CREATE.

I do not want to put this cumbersome assignment within my function. At present, I'm simply iterating through the structure and reassigning the fonts to the hFont member.

But I am wondering if there is a better way. I have several requirements, though...

1. I do not want to pass an int to a function, i.e,, I want to determine the size of the struct array dynamically.

2. I do not want to mimic the structure by doing something like MYSTRUCT ms2, or what-have-you.

IOW, I want to get my structure definition to recognize my loaded fonts without having to go back and iterate through it to assign the loaded fonts.

I thought I would try passing the structure by reference, or by a return value, but in most cases I can't get it to compile, and on the rare occassions it does compile, the program crashes.

I hope I'm being clear. If not, please let me know and I'll try again.
Aug 20, 2009 at 10:16pm
Actually, to be a little more clear, the following will give you an idea of what I'm trying to accomplish. I know the following won't work or compile, but it's indicative of what I'm looking for, using, of course, the above struct declaration in a header file, but NOT including the definition...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "mystruct.h"

MYSTRUCT GetStruct();

int main()
{
   MYSTRUCT* my=GetStruct();
   return 0;
}

MYSTRUCT GetStruct()
{
    MYSTRUCT mss[]= {
          "aaa", hCourNew;
         "bbb",  hCourNew;
         "ccc",   hCourNew;
         "ddd", hArial;
         "eee", hArial;
...etc.\};

   return mss;
}


As I said, the above is diastrous, but I hope it gives you a hint as to what I'm trying to accomplish, which is ONE structure definition such as in GetStruct() where the fonts, which are loaded in WM_CREATE, get assigned to the hFont member AFTER the fonts are loaded.
Aug 21, 2009 at 1:13am
IOW, I want to get my structure definition to recognize my loaded fonts without having to go back and iterate through it to assign the loaded fonts.


Sorry, that's impossible. You can't generate information before you know what it is.
Aug 21, 2009 at 1:44am
Okay, thanks. Iterating through is not a big deal. But with a pointer of type int, or char, or whatever, I can pass it by reference before I know what it is, so I thought maybe you could do the same with a structure. For example...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Display(int* inum)
{
   *inum = 7;
}

int main()
{
   int count;

  Display(&count);
  cout << count; // output will be "7"

  return 0;
}


Or of course you could make the function itself return the pointer, i.e., instead of void Display you make int Display() and return the pointer that way.

So there's no way to do the same with a structure?
Last edited on Aug 21, 2009 at 1:53am
Aug 21, 2009 at 2:59am
1
2
3
4
5
6
7
struct myStruct {
    //...
}

void Display(myStruct* ms) {
    //...
}


Doesn't this work? (just make sure to have the class definition before you use any of it's members).
Aug 21, 2009 at 3:55am
Yes, this will work as long as I know the structure size beforehand. But if I add or remove members from the structure, I see no way to dynamically define the size so that in the calling function I could do something like --

MYSTRUCT ms[iSTRUCTSIZE];

I have to know iSTRUCTSIZE before I declare the stuct in the calling function.

It would be nice if I could do something like -

MYSTRUCT* ms;

and then -

void Display(MYSTRUCT** mmss)

but I get creamed by the compiler when I try this, or any variation thereof.

At the end of the day I think that I'm just going to have to be satisfied with iterating through it, which is not that big of a deal.

I am still pretty new to programming, so I'm always trying to push the envelope in order to learn. Which means I can do some pretty stupid things sometimes.

Aug 21, 2009 at 4:26am
1
2
3
4
5
template< size_t N >
void Display( myStruct ms[ N ] )
{
    std::cout << "Array has " << N << " elements." << std::endl;
}


?
Aug 21, 2009 at 4:52am
Thank you. That's moving into C++, but I need to do exactly that in some things. After all, these "new" additions that have been incorporated into C++ over the years ain't all bad! -:)
Topic archived. No new replies allowed.