469,927 Members | 1,655 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Passing a struct as reference

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
Jul 22 '05 #1
5 2082
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2

"uny ternally" <un********@hotmail.com> wrote in message
news:aa*************************@posting.google.co m...
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
Jul 22 '05 #3
"Alf P. Steinbach" <al***@start.no> wrote in message
news:41****************@news.individual.net...
* 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.


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

--
Unforgiven

Jul 22 '05 #4
"John Harrison" <jo*************@hotmail.com> wrote:
"uny ternally" <un********@hotmail.com> wrote:
typedef struct conf
{
LPTSTR entry1;
int entry2;
};
bool GetConf(conf& config)
{
CString value;
LPTSTR val = value.GetBuffer(1000);
config.entry2 = val;


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).
Jul 22 '05 #5
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.
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Dave | last post: by
5 posts views Thread by kazack | last post: by
4 posts views Thread by Pushkar Pradhan | last post: by
9 posts views Thread by Just Me | last post: by
6 posts views Thread by MSDNAndi | last post: by
12 posts views Thread by Mike | last post: by
22 posts views Thread by tshad | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.