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 6079469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:56:59+00:00 2026-05-23T10:56:59+00:00

Possible Duplicate: Calculating size of an array This question has been asked before, but

  • 0

Possible Duplicate:
Calculating size of an array

This question has been asked before, but I want to confirm it. Let’s say I have the following C++ function:

#include <stdio.h>
#include <stdin.h>
#define length(a) sizeof(a)/sizeof(a[0])

int main()
{
    double c[] = {1.0, 2.0, 3.0};
    printf("%d\n", getLength(c));
    return 0;
}

int getLength(double c[]) { return length(c);}

This should return the wrong value because size of will return the size of the pointer as opposed to the size of the array being pointed at. This:

template<typename T, int size>
int length(T(&)[size]){return size;}

I know it works directly, but I want to know if I can somehow call it indirectly i.e. via a helper function. I understand that two possible alternatives are to either store the length of the array in a separate slot or use a vector, but what I want to know is:

Given a function getLength:

getLength(double arr[])
{
...
}

Is there a way to compute the length of arr without passing any more information?

  • 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-23T10:57:00+00:00Added an answer on May 23, 2026 at 10:57 am

    The template version would work. Who said it will not work?

    template<typename T, int size>
    int length(T(&)[size])
    {
       return size;
    }
    

    This is correct. It will return the length of the array. You just need to call this function directly. It seems you want to call this function from getLength(). Don’t do that, because the way you’ve written getLength function is equivalent to this:

    int getLength(double *c) { return length(c);}
    

    There is absolutely no difference between your written getLength() and the above version. That is, once you call this function, (or your function), the array is already decayed into a pointer to double. You’ve already lost the size information with the decay of the array (which is why this is called decaying of array). So you should NOT call this function, instead call the length() function directly..

    However, there is little problem with length() as well. You cannot use this function where const-expression is needed, as such:

    int anotherArray[length(c) * 10]; //error
    

    So the solution would be this:

    template < std::size_t N >
    struct value2type { typedef char type[N]; };
    
    template < typename T, std::size_t size >
    typename value2type<size>::type& sizeof_array_helper(T(&)[size]);
    
    #define length(a) sizeof(sizeof_array_helper(a))
    

    Now you can write:

    int anotherArray[length(c) * 10]; //ok
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Calculating the Difference Between Two Java Date Instances In Java, I want
Possible Duplicate: Problem in calculating checksum : casting int to signed int32 This should
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: What Ruby IDE do you prefer? I've generally been doing stuff on
Possible Duplicate: “BigInt” in C? Hey there! I'm calculating the Fibonacci numbers in C
Possible Duplicate: array_splice() for associative arrays How to add an array value to the
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: Simple statistics - Java packages for calculating mean, standard deviation, etc I
Possible Duplicate: What is 0x10 in decimal? I notice that Console.WriteLine(18); writes 18, but
Possible Duplicate: Calculating the Difference Between Two Java Date Instances hi, I have two

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.