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

The Archive Base Latest Questions

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

I am a little bit confused about pass an array in C/C++. I saw

  • 0

I am a little bit confused about pass an array in C/C++. I saw some cases in which the signature is like this

void f(int arr[])

some is like this

void f(int arr[], int size)

Could anybody elaborate what’s the difference and when and how to use it?

  • 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-23T15:56:09+00:00Added an answer on May 23, 2026 at 3:56 pm

    First, an array passed to a function actually passes a pointer to the first element of the array, e.g., if you have

    int a[] = { 1, 2, 3 };
    f(a);
    

    Then, f() gets &a[0] passed to it. So, when writing your function prototypes, the following are equivalent:

    void f(int arr[]);
    void f(int *arr);
    

    This means that the size of the array is lost, and f(), in general, can’t determine the size. (This is the reason I prefer void f(int *arr) form over void f(int arr[]).)

    There are two cases where f() doesn’t need the information, and in those two cases, it is OK to not have an extra parameter to it.

    First, there is some special, agreed value in arr that both the caller and f() take to mean “the end”. For example, one can agree that a value 0 means “Done”.

    Then one could write:

    int a[] = { 1, 2, 3, 0 }; /* make sure there is a 0 at the end */
    int result = f(a);
    

    and define f() something like:

    int f(int *a)
    {
        size_t i;
        int result = 0;
        for (i=0; a[i]; ++i)  /* loop until we see a 0 */
            result += a[i];
        return result;
    }
    

    Obviously, the above scheme works only if both the caller and the callee agree to a convention, and follow it. An example is strlen() function in the C library. It calculates the length of a string by finding a 0. If you pass it something that doesn’t have a 0 at the end, all bets are off, and you are in the undefined behavior territory.

    The second case is when you don’t really have an array. In this case, f() takes a pointer to an object (int in your example). So:

    int change_me = 10;
    f(&change_me);
    printf("%d\n", change_me);
    

    with

    void f(int *a)
    {
        *a = 42;
    }
    

    is fine: f() is not operating on an array anyway.

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

Sidebar

Related Questions

I'm a little bit confused about simple program which I wrote, can You please
I'm a little bit confused about how to approach this. I had never used
I'm a little bit confused about which method to use : 1- using Jquery
I'm a little bit confused about this expression: char *s = abc; Does the
I am a little bit confused about 2 things. Firstly when I create an
I´m a little bit confused by reading all the posts and tutorials about starting
I'm a little bit confused about these two terms, can somebody explain what is
I'm using SQLite as my database, and I'm a little bit confused about how
I'm a little bit confused about Git. When I'm looking through the manual it
Ok. Im little bit confused about those permissions in linux so please people help

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.