|
-
April 11th, 2003, 11:44 AM
#4
Originally posted by Clarke Kent
Thx Paul. That helps.
Actually, what im doing is trying to parse this structure...
struct FOO{
int count
LPCTSTR* names;
LPCTSTR* addresses;
}
So im receiveing this structure from another module and i dont know how to deal with it.
Im trying to format it like so....
Code:
TCHAR names[5];
TCHAR address[5];
for( i = 0; i< foo.count; ++i ) {
names[i] = foo.names[i];
addresses[i] = foo.addresses[i];
}
I'm under the assumption that you have control over the FOO structure, i.e. how it is defined.
The names and address TCHAR arrays are just that -- arrays of TCHAR. They are not pointers. In your loop, you are assigning a pointer value "foo.names[i]" to a single character, names[i]. This is incorrect. What you want to do is to copy all of the characters from foo.names[i] to the names character array (similarly with the address).
Code:
#include <string.h>
TCHAR names[5];
TCHAR address[5];
for( i = 0; i< foo.count; ++i ) {
strcpy( names, foo.names[i]);
strcpy(addresses, foo.addresses[i]);
//...
}
Your program has also other problems.
First, how long is each name? Is it a maximum of 4 characters (plus the NULL)? That is what your code suggests when you say names[5]. If you happen to have "Peter" as a name, you will be overwriting memory.
Second, I would have defined a single structure that takes one name and one address, and have an array of those. The problem with your code is that you're assuming that the number of names matches the number of addresses. In your loop, you may access a valid name, but an invalid address field, possibly causing a crash. This is of course if you can solidly, without a doubt, verify that for each name, there is an address and vice-versa.
But in general, the problem that I see is that string handling is too darn error prone and newcomers never have a comfortable time handling them. If you are coding in C++, you can relieve all of these problems by using std::string and std::vector instead of hard-coded arrays. Here is an example:
Code:
#include <string>
#include <vector>
#include <iostream>
struct FOO
{
std::string name;
std::string address;
};
typedef std::vector<FOO> NameAddressList;
//...
using namespace std;
void WriteNames(const NameAddressList& alist)
{
for( int i = 0; i< alist.size(); ++i ) {
cout << alist[i].name << endl;
cout << alist[i].address << endl;
}
}
int main()
{
NameAddressList nalist;
FOO foo;
// First name and address
foo.name = "John";
foo.address = "123 Main Street";
// add it to vector
nalist.push_back( foo );
// Second name and address
foo.name = "Mary";
foo.address = "100 First Street";
nalist.push_back( foo );
// Third name -- note that the address is empty
foo.name = "A very long name of someone with no address";
nalist.push_back( foo );
WriteNames(nalist);
}
With the code above you've
a) solved the problem of having names and addresses of any size
b) Your arrays are not fixed sized, since you are not using arrays
c) solved the issue if there is no address that has been defined for a name, or vice-versa (since a std::string is "" if nothing has been defined).
If you are coding in 'C', then you have to make provisions (in other words, write more code) for things such as names of unlimited (or maximum length), making sure you check you don't access invalid memory, etc. The code I wrote above takes care of all of those details (which is why it is really hard to go back to C coding when you have C++ with all of its niceties 
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|