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. sizeof string array in for loop?

sizeof string array in for loop?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelpquestion
7 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.
  • A Offline
    A Offline
    ALLERSLIT
    wrote on last edited by
    #1

    Hey, sorry if the threadtitle is a bit in-informative but I didn't know what to call it. Ok now to my problem, I am playing around with arrays and stuff see here ->

    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
    string a[] = { "one", "two", "three" };

    for (int i = 0;i <3;i++)
    {
        cout << a\[i\] << endl;
    }
    cin.get();
    

    }

    Now my problem is the "i < 3" let's say I wouldn't want to change that everytime i add another string to my array. How would I do that? Greetings!

    C S A 3 Replies Last reply
    0
    • A ALLERSLIT

      Hey, sorry if the threadtitle is a bit in-informative but I didn't know what to call it. Ok now to my problem, I am playing around with arrays and stuff see here ->

      #include <iostream>
      #include <string>

      using namespace std;

      int main()
      {
      string a[] = { "one", "two", "three" };

      for (int i = 0;i <3;i++)
      {
          cout << a\[i\] << endl;
      }
      cin.get();
      

      }

      Now my problem is the "i < 3" let's say I wouldn't want to change that everytime i add another string to my array. How would I do that? Greetings!

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      there is a ARRAYSIZE macro you can use : ARRAYSIZE(a). #define ARRAYSIZE(a) sizeof(a)/sizeof(a[0]) i don't recommend it. but it should work.

      image processing toolkits | batch image processing

      A 1 Reply Last reply
      0
      • A ALLERSLIT

        Hey, sorry if the threadtitle is a bit in-informative but I didn't know what to call it. Ok now to my problem, I am playing around with arrays and stuff see here ->

        #include <iostream>
        #include <string>

        using namespace std;

        int main()
        {
        string a[] = { "one", "two", "three" };

        for (int i = 0;i <3;i++)
        {
            cout << a\[i\] << endl;
        }
        cin.get();
        

        }

        Now my problem is the "i < 3" let's say I wouldn't want to change that everytime i add another string to my array. How would I do that? Greetings!

        S Offline
        S Offline
        Sauro Viti
        wrote on last edited by
        #3

        If you are using Visual C++ you can use the macro _countof(x) which returns the size of the specified array. In your case you have to write i < _countof(a)

        A 1 Reply Last reply
        0
        • C Chris Losinger

          there is a ARRAYSIZE macro you can use : ARRAYSIZE(a). #define ARRAYSIZE(a) sizeof(a)/sizeof(a[0]) i don't recommend it. but it should work.

          image processing toolkits | batch image processing

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

          Thanks!

          1 Reply Last reply
          0
          • S Sauro Viti

            If you are using Visual C++ you can use the macro _countof(x) which returns the size of the specified array. In your case you have to write i < _countof(a)

            A Offline
            A Offline
            ALLERSLIT
            wrote on last edited by
            #5

            I actually am using VC++, thanks for your answer!

            S 1 Reply Last reply
            0
            • A ALLERSLIT

              I actually am using VC++, thanks for your answer!

              S Offline
              S Offline
              Sauro Viti
              wrote on last edited by
              #6

              You are welcome! :thumbsup:

              1 Reply Last reply
              0
              • A ALLERSLIT

                Hey, sorry if the threadtitle is a bit in-informative but I didn't know what to call it. Ok now to my problem, I am playing around with arrays and stuff see here ->

                #include <iostream>
                #include <string>

                using namespace std;

                int main()
                {
                string a[] = { "one", "two", "three" };

                for (int i = 0;i <3;i++)
                {
                    cout << a\[i\] << endl;
                }
                cin.get();
                

                }

                Now my problem is the "i < 3" let's say I wouldn't want to change that everytime i add another string to my array. How would I do that? Greetings!

                A Offline
                A Offline
                Aescleal
                wrote on last edited by
                #7

                What's wrong with asking the compiler how big the array is - it knows, it bunged the thing on the stack for you:

                template <typename T, std::size_t N>
                std::size_t sizeof_array( T (&)[N] )
                {
                return N;
                }

                Most decent compilers (VC++2003, 2005, 2008 and 2010, gcc 3.3 onwards) will inline that to a constant. You can extend it a bit...

                template <typename T, std::size_t N>
                T *beginning_of_array( T (&a)[N] )
                {
                return a;
                }

                template <typename T, std::size_t N>
                T *end_of_array( T (&a)[N] )
                {
                return &a[ N ];
                }

                which means you can get away from using indexes entirely and start using algorithms to do the sort of thing you're doing in your example:

                int main()
                {
                std::string a[] = { "One", "Two", "Three" };

                std::copy( beginning\_of\_array( a ), end\_of\_array( a ), std::ostream\_iterator<std::string>( std::cout, "\\n" ) );
                

                }

                From there you're not far away from using overloaded functions to come up with a generic way of handling different aggregates of things. Cheers, Ash

                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