473,383 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 2430
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Dave | last post by:
I'm currently working on a small project (admitedly for my CS class) that compares the time difference between passing by value and passing by reference. I'm passing an array of 50000 int's. ...
5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
5
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a...
4
by: Pushkar Pradhan | last post by:
I need to pass a struct by reference to a function: The struct: typedef struct tifftags { uint32 imageWidth, ...; ... } TIFFTAGS; typedef TIFFTAGS * TIFFTAGS_PTR; main() { .....
9
by: Just Me | last post by:
PARAFORMAT2 is a structure that SendMessage will return stuff in. Is the "ref" correct or since only a pointer is being passed should it be by value? Suppose I was passing data rather then...
6
by: MSDNAndi | last post by:
Hi, I get the following warning: "Possibly incorrect assignment to local 'oLockObject' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the...
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
22
by: tshad | last post by:
If I am passing a variable by reference to another routine by reference, do I need to dereference first? string testString; .... FirstSub(ref firstString) { HandleString(ref firstString); ...
11
by: abhiM | last post by:
I have a struct that has an array in it. I need to assign space to the array in a function and pass the corresponding struct by reference to another function so that it can store values into the...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.