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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:26:29+00:00 2026-06-17T12:26:29+00:00

I’m currently studying C programming using the textbook C: How to program 6/e .

  • 0

I’m currently studying C programming using the textbook C: How to program 6/e.

Now, in the book when discussing arrays, it states that when passing an array, (other than a character array, due to the ‘\0’ character) to a function, it is necessary to pass the number of elements in the array to the function as well

So given my functions

int myfunct(int myary[], int numelements);

int myfunct(int *myary, int numelements);

Assume that ary has been declared with 10 elements and that we are now in the main function.

myfunct(ary, 10); 

Is the (int numelements necessary [in either case], and if so why). The book states that it is for the compiler to know the correct number of elements to process, however the book then goes on to not do this in almost all its examples, and is very very inconsistent.

Also,

int myfunct(int myary[][10], int numrows, int numcolumns);

Same question about numrows and numcolumns.

To illustrate further:

#include <stdio.h>
void test(const int ary[]);

int main(void){

    int myary[10] = {1,2,3,4,5,6,7,8,9,10};
    test(myary);

    return 0;
}

void test (const int ary[]){
    int i;

    for(i = 0; i<10; i++){
        printf("The number in array element %d is:    %d\n", i, ary[i] );
    }
}

This code seems to compile correctly without the additional parameter, but I do not understand why the book would say I need them, and so do many sites. HELP!

  • 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-17T12:26:30+00:00Added an answer on June 17, 2026 at 12:26 pm

    Is it necessary? Will my code compile if I don’t pass array size as argument?

    It is not necessary & your code will compile even if you do not pass array size as argument to the function.
    But it is a good practice to do so.

    Rationale:

    C does not provide you means of bounds checking for arrays. You can access beyond the bounds of array through array indexing or pointer arithmetic(Under the hood,both are same) and the compiler does not need to warn you about it. But you will end up getting an Undefined Behavior.
    So it is important that you only access memory which belongs to your array. To be able to do this you need to keep track of the size of the array.It is the user’s responsibility to do so.

    When you pass an array as argument it decays as pointer to first element of the array.
    So sizeof(arg) inside the function will give you the size of the pointer not the array.

    There is no way you can know the size of the array inside the function(exception of null terminated string, who’s size can be obtained by strlen) unless you pass it as an argument or through some other bookeeping(like global variable etc).

    Passing the size as an argument is the most obvious way to do this.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am confused How to use looping for Json response Array in another Array.
I am using JSon response to parse title,date content and thumbnail images and place
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

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.