Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9131401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:08:06+00:00 2026-06-17T08:08:06+00:00

So I have an array of strings. Here’s an example: std::string array[] = {"Example",

  • 0

So I have an array of strings. Here’s an example:

std::string array[] = {"Example", "Example2", "Example3"};

Is there any way I can find the number of elements in an array like the one above. I can’t use this method:

int numberofelements = sizeof(array)/sizeof(array[0]);

This is because the size of the elements vary. Is there another way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:08:07+00:00Added an answer on June 17, 2026 at 8:08 am

    Given your array of strings, you can most certainly use sizeof(array)/sizeof(array[0]) to get its size and the following program works just fine:

    int main()
    {
        std::string array[] = { "S1", "S2", "S3" };
        std::cout << "A number of elements in array is: "
                  << sizeof(array)/sizeof(array[0]) << '\n';
        foo(array);
    }
    

    It is not clear what do you mean by saying that size of elements vary. Size of the elements of any array is always known at compiler-time, no exceptions.

    There are, however, situations where the above will not work. Consider the following example:

    void foo(std::string array[])
    {
        std::cout << "A number of elements in array is: "
                  << sizeof(array)/sizeof(array[0]) << '\n';
    }
    

    The above code is doomed to fail. It might look a bit weird at first, but the reason for this is actually very simple — this is called array decaying. It means that every time you pass an array to a function, its type is automatically decayed to that of a pointer. So the above function is in fact an equivalent of this:

    void foo(std::string *array)
    {
    }
    

    And if in the first example the sizeof operator returns the total size of an array, in the second example it returns the size of a pointer to that array, which is a totally different thing.

    There are usually two ways people go about it. The first is to add a special “last” element of the array so that application can traverse the array until it sees the last element and calculate the array’s length. String literals are the perfect example of this — every string literal ends with ‘\0’ and you can always calculate its length. Here is an example:

    static void foo(const std::string *array)
    {
        size_t i = 0;
        while (!array[i].empty())
            ++i;
        std::cout << "Array length is: " << i << std::endl;
    }
    

    The downside is obviously a need to traverse the array to determine its length. The second way it to always carry array length around, for example:

    static void foo(const std::string *array, size_t length)
    {
        // ...
    }
    
    void bar()
    {
        std::string array[] = { "S1", "S2", "S3" };
        foo(array, sizeof(array)/sizeof(array[0]));
    }
    

    In C++, you can use a template to deduct array’s length, for example:

    template <size_t array_length>
    static void foo(const std::string (&array)[array_length])
    {
        std::cout << "A number of elements in template array is: "
                  << array_length << '\n';
    }
    

    All of the above applies to simple arrays that are built-in into the language. C++, on the other hand, provides a rich set of higher-level containers that give you a lot of flexibility. So you might want to consider using one of the containers that are available to you as part of C++ Standard Library. For a list of standard containers, see — http://en.cppreference.com/w/cpp/container

    Hope it helps. Good Luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the problem. I ,for example,have a string 2500.Its converted from byte array into
I have array of strings, String[] data and it's 10 elements has value P
I have an array of strings. How can I convert it to System.Collections.ArrayList?
I have an array of strings plus one additional string. I want to use
I have an array of strings(as shown in example). I just wish to find
I have the following string : [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],] How can I create a 2d array
I have an array of strings string[] tmp = foo(); If NONE of the
I have an array of strings like this: Some Title##DD-MM-JJJJ##Some Text goes here##img1.jpg##img2.jpg I'd
So I have two possible strings here for example. /user/name and /user/name?redirect=1 I'm trying
I have an array as the following: function example() { /* some stuff here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.