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

The Archive Base Latest Questions

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

The count is returning unpredictable results. Sometimes they are right. Sometimes totally weird. Anyone

  • 0

The count is returning unpredictable results. Sometimes they are right. Sometimes totally weird. Anyone can tell me what is wrong?

#include <stdio.h>
int len(int[]);

int main (int argc, const char * argv[])
{
    int a[]={1,2,3,4,5,6,7,8};
    int* i = a;
    printf("length is %d",(len(i)));
    return 0;
}

int len(int* a){
    int count = 0;
    for (; *a!='\0'; a++) {
        count++;
    }
    return count; 
}
  • 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-29T23:31:14+00:00Added an answer on May 29, 2026 at 11:31 pm

    I think you’re confused between C strings (arrays of char) and other arrays. It’s a convention that C strings are terminated with a null character ('\0'), but not all arrays (even char arrays) are terminated this way.

    The general convention is to either store the length of an array somewhere, or to use a sentinel value at the end of the array. This value should be one that won’t come up inside the array – eg '\0' in strings, or -1 in an array of positive ints.

    Also, if you know that a is an int array (and not a pointer to an int array), then you can use:

    size_t length = sizeof(a) / sizeof(a[0]);
    

    So you could do:

    int a[] = {1,2,3,4,5,6,7,8};
    size_t length = sizeof(a) / sizeof(a[0]); 
    
    // In this case, sizeof(a[0]) 
    // is the same as sizeof(int), because it's an int array.
    

    But you can’t do:

    int *a = malloc(sizeof(int) * 10);
    size_t length = sizeof(a) / sizeof(a[0]); // WRONG! 
    

    That last example will compile, but the answer will be wrong, because you’re getting the size of a pointer to the array rather than the size of the array.

    Note that you also can’t use this sizeof to read the size of an array that’s been passed into a function. It doesn’t matter whether you declare your function len(int *a) or len(int a[]) – a will be a pointer, because the compiler converts arrays in function arguments to be a pointer to their first element.

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

Sidebar

Related Questions

Possible Duplicate: Hibernate HQL: Get count of results without actually returning them I have
I am returning form elements into a form from ajax. I can count the
How can I count the number of elements in an array, because contrary to
How can I count operations in C++? I'd like to analyze code in a
How can I tell (through the debugger if my app resources are being loaded
The query below is not returning any results. I have copied it from a
I want to get the count of the results of a dynamically-generated HQL query,
this count is onyl returning 1, when there are thousands of records in this
Why are these two statements returning the same results? I'm looking at the total
select station_id,count(case_id) from emer_complaint group by station_id Above query returning the correct result. But,

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.