Passing a struct as reference

U

uny ternally

I was experimenting in Visual C++ and ran into the following problem.
I have the struct listed below. I also have a function that passes a
variable of the struct type by reference and set the values. When I
access the struct in the global space after calling the function the
integer value is correcty, however the LPTSTR value is empty. Help.
Below is the code I'm playing with.

Thanks.

typedef struct conf
{
LPTSTR entry1;
int entry2;
};

bool GetConf(conf& config)
{
CString value;
DWORD dwSize = MAX_PATH;
DWORD val2;

LPTSTR val = value.GetBuffer(1000);
// code setting val and val2
config.entry1 = val2;
config.entry2 = val;
std::cout << "inside: " << config.entry2 << "\n";
// config.entry2 dispalys properly here
return TRUE;
}

.... inside of main

bool returnval;
conf cony;

returnval = GetConf(cony);
std::cout << "outside: " << cony.entry2 << "\n";
\\here cony.entry2 is empty. Note if both cout's are changed from
entry2 to entry they work correctly both inside and outside.

thanks again
 
A

Alf P. Steinbach

* uny ternally:
I was experimenting in Visual C++ and ran into the following problem.
I have the struct listed below. I also have a function that passes a
variable of the struct type by reference and set the values. When I
access the struct in the global space after calling the function the
integer value is correcty, however the LPTSTR value is empty.

'LPTSTR' is not a C++ type: you should provide a definition so that all
readers of your posting can know what you're talking about.

Help.
Below is the code I'm playing with.

Thanks.

typedef struct conf
{
LPTSTR entry1;
int entry2;
};

'entry1' and 'entry2' are bad names.

They do not say anything about what they stand for.

Use better names; that will help you also with your current problem.

Also, are you sure this compiles?

A 'typedef' is of the form 'typedef TYPESPEC NAME', and you have only
the TYPESPEC not the NAME.

A 'typedef' is not necessary for a 'struct'.

In C++ the name of a 'struct' is a general type.

bool GetConf(conf& config)

Consider using a constructor instead of a function like this.

{
CString value;
DWORD dwSize = MAX_PATH;
DWORD val2;

CString and DWORD are not built-in C++ types.
LPTSTR val = value.GetBuffer(1000);
// code setting val and val2
config.entry1 = val2;

Is 'entry1' compatible with a DWORD type?

Does this compile?

What does 'GetBuffer' do?

config.entry2 = val;
std::cout << "inside: " << config.entry2 << "\n";
// config.entry2 dispalys properly here
return TRUE;

Use C++ 'true' unless TRUE is defined as 'false'.
}

... inside of main

bool returnval;
conf cony;

returnval = GetConf(cony);
std::cout << "outside: " << cony.entry2 << "\n";
\\here cony.entry2 is empty. Note if both cout's are changed from
entry2 to entry they work correctly both inside and outside.

If what you write is true then you haven't shown all relevant code.
 
J

John Harrison

uny ternally said:
I was experimenting in Visual C++ and ran into the following problem.
I have the struct listed below. I also have a function that passes a
variable of the struct type by reference and set the values. When I
access the struct in the global space after calling the function the
integer value is correcty, however the LPTSTR value is empty. Help.
Below is the code I'm playing with.

Your problem is nothing to do with references. Instead its a
misunderstanding of object scope and pointers. Explaination below.
Thanks.

typedef struct conf
{
LPTSTR entry1;
int entry2;
};

This is not legal C++. You are mixing up C and C++ and getting it wrong for
both. Lose the typedef.
bool GetConf(conf& config)
{
CString value;

A CString is created here.
DWORD dwSize = MAX_PATH;
DWORD val2;

LPTSTR val = value.GetBuffer(1000);
// code setting val and val2
config.entry1 = val2;
config.entry2 = val;
std::cout << "inside: " << config.entry2 << "\n";

You mean entry1.
// config.entry2 dispalys properly here
return TRUE;

That CString is destroyed here.
}

... inside of main

bool returnval;
conf cony;

returnval = GetConf(cony);
std::cout << "outside: " << cony.entry2 << "\n";
\\here cony.entry2 is empty. Note if both cout's are changed from
entry2 to entry they work correctly both inside and outside.

Again you mean entry1.

Now conv.entry1 is a pointer pointing to the internals of a CString object
that has already been destroyed. That's why it is 'empty'. You are lucky
that your program doesn't crash.

I would recommend not useing pointers here (in fact I would recommend that
newbies avoid pointers altogther).

Try this code

struct conf
{
CString entry1;
int entry2;
};

bool GetConf(conf& config)
{
CString value;
DWORD val2;
...
// code setting val and val2
config.entry1 = val2;
config.entry2 = value;
std::cout << "inside: " << config.entry2 << "\n";
// config.entry2 dispalys properly here
return TRUE;
}

No pointers, no problem.

Also I recommend that you drop CString and use the superior and standard C++
string class instead.

john
 
U

Unforgiven

Alf P. Steinbach said:
* uny ternally:

'LPTSTR' is not a C++ type: you should provide a definition so that all
readers of your posting can know what you're talking about.

#ifdef UNICODE
typedef wchar_t* LPTSTR
#else
typedef char* LPTSTR
#endif
 
O

Old Wolf

John Harrison said:
Try this code

struct conf
{
CString entry1;
int entry2;
};
bool GetConf(conf& config)
{
CString value;
config.entry2 = value;

The OP assigned a char* to an int, now you are assigning a CString
to an int, am I missing something? (unless CString has an operator
int(), which wouldn't surprise me).
 
U

uny ternally

Thanks, for all the suggestions. Changing the struct to use the C++
string class cleared things up. ALso sorry about the typos in the
code it was a bad cut a paste job, pulling just the relevant code out
a a bigger base.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top