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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:07:54+00:00 2026-05-27T05:07:54+00:00

This is well known code to compute array length in C: sizeof(array)/sizeof(type) But I

  • 0

This is well known code to compute array length in C:

sizeof(array)/sizeof(type)

But I can’t seem to find out the length of the array passed as an argument to a function:

#include <stdio.h>

int length(const char* array[]) {
  return sizeof(array)/sizeof(char*);
}

int main() {
  const char* friends[] = { "John", "Jack", "Jim" };
  printf("%d %d", sizeof(friends)/sizeof(char*), length(friends)); // 3 1
}

I assume that array is copied by value to the function argument as constant pointer and reference to it should solve this, but this declaration is not valid:

int length(const char**& array);

I find passing the array length as second argument to be redundant information, but why is the standard declaration of main like this:

int main(int argc, char** argv);

Please explain if it is possible to find out the array length in function argument, and if so, why is there the redundancy in main.


  • 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-27T05:07:55+00:00Added an answer on May 27, 2026 at 5:07 am

    sizeof only works to find the length of the array if you apply it to the original array.

    int a[5]; //real array. NOT a pointer
    sizeof(a); // :)
    

    However, by the time the array decays into a pointer, sizeof will give the size of the pointer and not of the array.

    int a[5];
    int * p = a;
    sizeof(p); // :(
    

    As you have already smartly pointed out main receives the length of the array as an argument (argc). Yes, this is out of necessity and is not redundant. (Well, it is kind of reduntant since argv is conveniently terminated by a null pointer but I digress)

    There is some reasoning as to why this would take place. How could we make things so that a C array also knows its length?

    A first idea would be not having arrays decaying into pointers when they are passed to a function and continuing to keep the array length in the type system. The bad thing about this is that you would need to have a separate function for every possible array length and doing so is not a good idea. (Pascal did this and some people think this is one of the reasons it “lost” to C)

    A second idea is storing the array length next to the array, just like any modern programming language does:

    a -> [5];[0,0,0,0,0]
    

    But then you are just creating an invisible struct behind the scenes and the C philosophy does not approve of this kind of overhead. That said, creating such a struct yourself is often a good idea for some sorts of problems:

    struct {
        size_t length;
        int * elements;
    }
    

    Another thing you can think about is how strings in C are null terminated instead of storing a length (as in Pascal). To store a length without worrying about limits need a whopping four bytes, an unimaginably expensive amount (at least back then). One could wonder if arrays could be also null terminated like that but then how would you allow the array to store a null?

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

Sidebar

Related Questions

I can't seem to find an answer to this anywhere. It's well known that
Sorry for the unhelpful title, but hopefully I can explain this well enough. Lets
When you run perl -e "Bla->new" , you get this well-known error: Can't locate
Sorry if this is well known but Googling for my answer only came up
I don't know if this is a well known 'thing' or something new in
I was reading about this person's interview at a well-known search company. http://asserttrue.blogspot.com/2009/05/one-of-toughest-job-interview-questions.html He
I know this question may well be the silliest question you've heard today, but
I know SQL well but I must be missing something really dumb here. This
I hope I can explain this well enough so that you can understand the
This question may seem rather basic, but coming from an engineering (non computer-science) background,

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.