Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to find how many strings are present?

How to find how many strings are present?

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++tutorialquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kapardhi
    wrote on last edited by
    #1

    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!

    C G A 3 Replies Last reply
    0
    • K kapardhi

      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!

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      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++

      1 Reply Last reply
      0
      • K kapardhi

        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!

        G Offline
        G Offline
        Garth J Lancaster
        wrote on last edited by
        #3

        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'

        1 Reply Last reply
        0
        • K kapardhi

          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!

          A Offline
          A Offline
          A_xin
          wrote on last edited by
          #4

          /*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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups