farzanj
asked on
C++ string array assignment
I am doing C++ after a long time. I am getting a little trouble in lines with comment of NEED HELP.
#include<iostream>
#include<string>
#include<cassert>
using namespace std;
struct Delims
{
string start[];
string end[];
int tok_len[];
Delims(string s[], string e[])
{
assert ( sizeof(s) == sizeof(e) );
start = s; // NEED HELP
end = e; // NEED HELP
//Check the lengths
for (int i=0 ; i < sizeof(s)/sizeof(string) ; i++)
{
tok_len[i] = start[i].size();
}
}
};
int main()
{
string x[] = {"ABC","XYZ"};
string y[] = {",", ","};
Delims a(x,y);
return 0;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi JKR,
sizeof(s)/sizeof(string) tells the number of elements in an array. Sorry, I still have old touch in my code since I have not touched C++ for more than a decade now.
sizeof(s)/sizeof(string) tells the number of elements in an array. Sorry, I still have old touch in my code since I have not touched C++ for more than a decade now.
Well, then you can just make that
for (int i=0 ; i < s.size(); i++)
{
tok_len[i] = start[i].size();
}
ASKER
Well, your suggestions are great and I would certainly incorporate it. But, please also tell me why my original code did not compile. How should I fix that, just for academic reason. You guys are thinking at an advanced level and since I have been out of touch for a very long time, so I am struggling with basic assignments of arrays & pointers syntax.
Hm - I'm not sure if I can phrase it correctly (some others here do this better), but I'll try: IMO it's not possible to implement a C-style array as member in the form of '<type> <name>[]' (so without knowing the size of the C-style array at compile time). you could re-write the code using pointers as members in the form '<type>* <name>' - in this case you have to take care to allocate and free memory needed for the array. Further it's not possible to use '=' to assign data to a C-style array after it was created. So i.e. it's possible to do something like 'int n[] = { 1, 2, 3 };', but it's not possible to do something like
ZOPPO
int n[] = { 1, 2, 3 };
int m[];
m = n
Unfortunateley I have not really enough time just now to find some good resources since I have to catch my train, but I guess anyone else here will give better explanation soon :o)ZOPPO
http://pw1.netcom.com/~tjensen/ptr/pointers.htm
Because this
start = s; // NEED HELP Here you are trying to assign to the constant pointer start a pointer s. S points the first element of array s;
end = e; // NEED HELP
is not correct.
in C++ the name of a string is a constant pointer to the first element of the string. You should use http://www.cplusplus.com/reference/clibrary/cstring/strcpy/.
string start[];
string end[];
int tok_len[];
Delims(string s[], string e[])
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks everyone. But Sara said what I was actually looking for and had forgotten.
ASKER
This is certainly good help. However, since I am a very old time C++ programmer, I want to recall it too. Could you please tell why my previous program did not compile?