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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:56:10+00:00 2026-05-13T10:56:10+00:00

when I need to pass an array to a function, it seems all the

  • 0

when I need to pass an array to a function, it seems all the following declarations of the function will work

void f(int arr[])  
void f(int arr[4]) // is this one correct?

for this:

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

But when I assign an array to another array, it fails

int a[]={1,2,3,4};
int b[4] = a; // error: array must be initialized with a brace-enclosed initializer

So why an array passed as an argument of a function is okay, but used on the rhs of simple assignment is wrong?

  • 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-13T10:56:11+00:00Added an answer on May 13, 2026 at 10:56 am

    For understanding the difference, we need to understand two different contexts.

    • In value contexts, the name of an array of type T is equivalent to a pointer to type T, and is equal to a pointer to the array’s first element.
    • In object contexts, the name of an array of type T does not reduce to a pointer.

    What is object context?

    In a = b;, a is in object context. When you taken the address of a variable, it’s used in object context. Finally, when you use sizeof operator on a variable, it’s used in object context. In all other cases, a variable is used in value context.

    Now that we have this knowledge, when we do:

    void f(int arr[4]);
    

    It is exactly equivalent to

    void f(int *arr);
    

    As you found out, we can omit the size (4 above) from the function declaration. This means that you can’t know the size of the “array” passed to f(). Later, when you do:

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

    In the function call, the name a is in value context, so it reduces to a pointer to int. This is good, because f expects a pointer to an int, so the function definition and use match. What is passed to f() is the pointer to the first element of a (&a[0]).

    In the case of

    int a[]={1,2,3,4};
    int b[4] = a;
    

    The name b is used in a object context, and does not reduce to a pointer. (Incidentally, a here is in a value context, and reduces to a pointer.)

    Now, int b[4]; assigns storage worth of 4 ints and gives the name b to it. a was also assigned similar storage. So, in effect, the above assignment means, “I want to make the storage location the same as the previous location”. This doesn’t make sense.

    If you want to copy the contents of a into b, then you could do:

    #include <string.h>
    int b[4];
    memcpy(b, a, sizeof b);
    

    Or, if you wanted a pointer b that pointed to a:

    int *b = a;
    

    Here, a is in value context, and reduces to a pointer to int, so we can assign a to an int *.

    Finally, when initializing an array, you can assign to it explicit values:

    int a[] = {1, 2, 3, 4};
    

    Here, a has 4 elements, initialized to 1, 2, 3, and 4. You could also do:

    int a[4] = {1, 2, 3, 4};
    

    If there are fewer elements in the list than the number of elements in the array, then the rest of the values are taken to be 0:

    int a[4] = {1, 2};
    

    sets a[2] and a[3] to 0.

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

Sidebar

Ask A Question

Stats

  • Questions 370k
  • Answers 370k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Once you have selected a radio button, there's really no… May 14, 2026 at 6:37 pm
  • Editorial Team
    Editorial Team added an answer With Telerik and .NET in general you'l find the code… May 14, 2026 at 6:37 pm
  • Editorial Team
    Editorial Team added an answer No, but you can use the Regex class. Code to… May 14, 2026 at 6:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.