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. string arrays in c++

string arrays in c++

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++data-structureshelptutorial
6 Posts 5 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.
  • M Offline
    M Offline
    mrspam
    wrote on last edited by
    #1

    Consider the following: string mystring[] = {"Hello","World"}; How do I find how many elements my array of has? If for example I wanted to loop through the array and count the elements that had the letter 'L' or 'l' in them. displaying something like: element 1: 2 element 2: 1 My main focus here is how do I find out the number if elements in a given array. Thanks in advance for your help.

    A B P 3 Replies Last reply
    0
    • M mrspam

      Consider the following: string mystring[] = {"Hello","World"}; How do I find how many elements my array of has? If for example I wanted to loop through the array and count the elements that had the letter 'L' or 'l' in them. displaying something like: element 1: 2 element 2: 1 My main focus here is how do I find out the number if elements in a given array. Thanks in advance for your help.

      A Offline
      A Offline
      antlers
      wrote on last edited by
      #2

      sizeof( ) is your friend here. Remember, it will give you the size of the whole array, so you have to divide by the size of each element.

      1 Reply Last reply
      0
      • M mrspam

        Consider the following: string mystring[] = {"Hello","World"}; How do I find how many elements my array of has? If for example I wanted to loop through the array and count the elements that had the letter 'L' or 'l' in them. displaying something like: element 1: 2 element 2: 1 My main focus here is how do I find out the number if elements in a given array. Thanks in advance for your help.

        B Offline
        B Offline
        BlackDice
        wrote on last edited by
        #3

        I know with regular arrays you would do a sizeof(mystring)/sizeof(string), but I really don't know about with string variable types. If it's broken, I probably did it bdiamond

        M 1 Reply Last reply
        0
        • B BlackDice

          I know with regular arrays you would do a sizeof(mystring)/sizeof(string), but I really don't know about with string variable types. If it's broken, I probably did it bdiamond

          M Offline
          M Offline
          mrspam
          wrote on last edited by
          #4

          Cool, What about if we take it one step further and have this: int main() { string mystring[] = {"ASD","asdf"}; int i; i = myfunc(mystring[]); cout << i; return 1; } int myfunc(string mystring[]) { int i; i = sizeof(mystring) / sizeof(mystring[0]; return i; } I get two different results when using the sizeof() in myfunc and sizeof in main.... Do I need to pass a pointer to the string??

          C 1 Reply Last reply
          0
          • M mrspam

            Cool, What about if we take it one step further and have this: int main() { string mystring[] = {"ASD","asdf"}; int i; i = myfunc(mystring[]); cout << i; return 1; } int myfunc(string mystring[]) { int i; i = sizeof(mystring) / sizeof(mystring[0]; return i; } I get two different results when using the sizeof() in myfunc and sizeof in main.... Do I need to pass a pointer to the string??

            C Offline
            C Offline
            Curi0us_George
            wrote on last edited by
            #5

            You must pass the array size to myfunc. Myfunc receives a string pointer (that's all an array really is, a pointer to a contiguous block of strings), and has no way of knowing the size of the array that pointer points to. Inside the main function, the compiler substitutes the sizeof operator for the actual known size of your string array. However, inside the myfunc function, the compiler has no way to determine the size of the array passed. If you print the sizeof(mystring) in myfunc, you'll find it always equals 4, the size in bytes of a pointer (on the x86 architecture). It would probably be better if sizeof() never worked on arrays at all. At least then it would be consistent and less likely to confuse people who've never run into this before.

            1 Reply Last reply
            0
            • M mrspam

              Consider the following: string mystring[] = {"Hello","World"}; How do I find how many elements my array of has? If for example I wanted to loop through the array and count the elements that had the letter 'L' or 'l' in them. displaying something like: element 1: 2 element 2: 1 My main focus here is how do I find out the number if elements in a given array. Thanks in advance for your help.

              P Offline
              P Offline
              Paul Ranson
              wrote on last edited by
              #6

              It might be preferable to find a way to use vector< string > mystring ; Then mystring.size () is obviously the size... Otherwise you could use a sentinel, string mystring [] = { "Hello", "World", "" } ; void DoSomething ( string mystring []) { while ( !mystring->empty ()) { DoSomethingWithMystring ( mystring ) ; ++mystring ; } } Paul

              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