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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:07:12+00:00 2026-06-14T10:07:12+00:00

I have tried (sizeof(array)/sizeof(array[0])) . Didn’t work. I wrote a simple function: int length(int

  • 0

I have tried (sizeof(array)/sizeof(array[0])). Didn’t work.
I wrote a simple function:

int length(int array[]){
    int i=0;
    while(array[i]) i++;
    return i;
}

Worked one minute, didn’t work the next.

Someone please help! I’m using Xcode as an IDE

  • 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-14T10:07:14+00:00Added an answer on June 14, 2026 at 10:07 am

    The length of an array is not part of the array in C, so when passing an array as a parameter to a function you should pass its length as a parameter too. Here’s an example:

    #define ARRLEN(a) (sizeof(a)/sizeof (a)[0]) /* a must be an array, not a pointer */
    
    void printarray(int* a, int alen)
    {
        int i;
        for (i = 0; i < alen; i++)
            printf("%d\n", a[i]);
    }
    
    main()
    {
        int a[] = { 3, 4, 5 };
        printarray(a, ARRLEN(a));
        return 0;
    }
    

    However, if your array is defined in such a way as to always end with a sentinel that isn’t normal data, then you can traverse the elements until you encounter the sentinel. e.g.,

    void printstrings(char** a)
    {
        int i;
        for (i = 0; a[i]; i++)
            printf("%s\n", a[i]);
    }
    
    main()
    {
        char* a[] = { "This", "should", "work.", NULL };
        printstrings(a);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried this: #define format(f, ...) \ int size = strlen(f) + (sizeof((int[]){__VA_ARGS__})/sizeof(int))
I have an array elsewhere in my program: data=malloc(sizeof(int)*lines); I want to read data
I have tried many permutations but they all don't seems to work well. Am
I have tried to create a simple controller/model (based on the samples code of
I have to create a function that can take in an array of pointers
I wanted to convert array< Byte>^ to unsigned char*. I have tried to explain
I have a very large array which must be 262144 elements in length (and
I have this array: unsigned char* data = CGBitmapContextGetData(cgctx); then I tried to get
I have tried to implement Stack Overflow question C++ Data Member Alignment and Array Packing
Have tried to find solutions for this and can't really come up with anything.

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.