How to find how many strings are present?
-
Hai! I have a string pointer in my MFC application, a function assigns some strings to this pointer. Now i want to extract the strings, i do it in following manner: CString *strArray; ... // Function call () that assigns some strings ... // code to extract each string from the pointer for (i = 0; strArray [i] != ""; i ++) { // i am extracting each string like strArray [i] } but the problem is when it comes to end of the pointer, assume that there are 4 strings, when the loop checks for 5 th string(which is not present), it gives an unhandeled exception error. when i debbugged, strArray[5] was pointing to "Bad Pointer" How to resolve this problem. Is there any better way to extract each string from a string pointer? Thanks!
-
Hai! I have a string pointer in my MFC application, a function assigns some strings to this pointer. Now i want to extract the strings, i do it in following manner: CString *strArray; ... // Function call () that assigns some strings ... // code to extract each string from the pointer for (i = 0; strArray [i] != ""; i ++) { // i am extracting each string like strArray [i] } but the problem is when it comes to end of the pointer, assume that there are 4 strings, when the loop checks for 5 th string(which is not present), it gives an unhandeled exception error. when i debbugged, strArray[5] was pointing to "Bad Pointer" How to resolve this problem. Is there any better way to extract each string from a string pointer? Thanks!
If you don't know how many strings will be stored in your array, I suggest you use a dynamic container, like a std::vector for instance. It will make your life much easier when working with a dynamic array. If you still want to stick to your solution, then you will need an extra variable which counts the number of strings in the array (and the array should be large enough also).
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
Hai! I have a string pointer in my MFC application, a function assigns some strings to this pointer. Now i want to extract the strings, i do it in following manner: CString *strArray; ... // Function call () that assigns some strings ... // code to extract each string from the pointer for (i = 0; strArray [i] != ""; i ++) { // i am extracting each string like strArray [i] } but the problem is when it comes to end of the pointer, assume that there are 4 strings, when the loop checks for 5 th string(which is not present), it gives an unhandeled exception error. when i debbugged, strArray[5] was pointing to "Bad Pointer" How to resolve this problem. Is there any better way to extract each string from a string pointer? Thanks!
As per Cedric's suggestions, or, surely there's an upper limit on either the number of strings strArray was defined to hold, or, this // Function call () that assigns some strings knows how many strings were assigned... This being the case, why dont you run your loop to [max_elements-1], and inside the loop check if strArray[n] == null 'g'
-
Hai! I have a string pointer in my MFC application, a function assigns some strings to this pointer. Now i want to extract the strings, i do it in following manner: CString *strArray; ... // Function call () that assigns some strings ... // code to extract each string from the pointer for (i = 0; strArray [i] != ""; i ++) { // i am extracting each string like strArray [i] } but the problem is when it comes to end of the pointer, assume that there are 4 strings, when the loop checks for 5 th string(which is not present), it gives an unhandeled exception error. when i debbugged, strArray[5] was pointing to "Bad Pointer" How to resolve this problem. Is there any better way to extract each string from a string pointer? Thanks!
/*Function call () that assigns some strings*/ CString * GetstrArray(int len) { CString *re; re=new CString[len]; //input your string return re; } CString *strArray; int len=4; strArray=GetstrArray(len); for(i=0;i<len&&strArray[i]!="";i++) { ::AfxMessageBox(strArray[i]); } delete []strArray; or /*Function call () that assigns some strings*/ CString * GetstrArray(int &len) { CString *re; int inlen=1; inlen=4;//you can change the value re=new CString[inlen]; //input your string len=inlen; return re; } CString *strArray; int i; int len; strArray=GetstrArray(len); for(i=0;i<len&&strArray[i]!="";i++) { ::AfxMessageBox(strArray[i]); } delete []strArray; modified on Friday, March 13, 2009 7:01 AM
modified on Friday, March 13, 2009 7:19 AM