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

  • SEARCH
  • Home
  • 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 664859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:38:44+00:00 2026-05-13T23:38:44+00:00

How in C++ get array length with pointers only ? I know that tab

  • 0

How in C++ get array length with pointers only ? I know that tab name is pointer to first element, but what next ?

  • 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-05-13T23:38:44+00:00Added an answer on May 13, 2026 at 11:38 pm

    You cannot. A pointer is just a memory location, and contains nothing special that could determine the size.

    Since this is C++, what you can do is pass the array by reference like so:

    template <typename T, size_t N>
    void handle_array(T (&pX)[N])
    {
        // the size is N
    
        pX[0] = /* blah */;
        // ...
        pX[N - 1] = /* blah */;
    }
    
    // for a specific type:
    template <size_t N>
    void handle_array(int (const &pX)[N]) // const this time, for fun
    {
        // the size is N
    
        int i = pX[0]; // etc
    }
    

    But otherwise you need to pass start & end and do a subtraction, like Alok suggests, a start & size, like you suggest, or ditch a static array and use a vector, like Tyler suggests.

    If you know the size of the array you’ll be working with, you can make a typedef:

    typedef int int_array[10];
    
    void handle_ten_ints(int_array& pX)
    {
        // size must be 10
    }
    

    And just for the size:

    template <typename T, size_t N>
    size_t countof(T (&pX)[N])
    {
        return N;
    }
    
    template <typename T, size_t N>
    T* endof(T (&pX)[N])
    {
        return &pX[0] + N;
    }
    
    // use
    int someArray[] = {1, 2, 6, 2, 8, 1, 3, 3, 7};
    
    size_t count = countof(someArray); // 9
    std::for_each(someArray, endof(someArray), /* ... */);
    

    I use these utility functions from time to time.

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

Sidebar

Related Questions

Is there a way to get the length of an Array when I only
I'm unable to get the length of the following JSON array object. Actually I
I need to get array of images that have to call from server and
I (think I) understand that you can only retrieve the size of an array
Let's say that any C function has a pointer already declared, but not assigned
How can I get an array of pointers pointing to objects (classes) ? I
Im getting a null pointer exception when I attempt to get an array of
It's a pointer to an array of LSA_UNICODE_STRING structures. I found some code that
I want to get the length of an array, say int array[] = {1,
How to get array data from json in c#? Here is the ajax code

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.