Empty array as argument to a function in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • swapnaoe
    New Member
    • Jul 2006
    • 21

    #1

    Empty array as argument to a function in C

    Hi all,

    An empty array declared in a called function is used as a pointer to hold the address of an object passed in the calling function. this is perfectly legal.
    {/*calling function*/
    ...
    my_function(&in t_variable);
    ...
    }


    /*my function definition*/
    my_function(int i[])
    {
    ......

    }

    my question is why doesnt the compiler recognize it as an empty array declaration and throw an error?? I have always assumed that the arguments in the definition of any unction are nothing but the declarations of its local objects.
    Please let me know if the question is not clear.

    Thanks
    Swapna
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by swapnaoe
    Hi all,

    An empty array declared in a called function is used as a pointer to hold the address of an object passed in the calling function. this is perfectly legal.
    {/*calling function*/
    ...
    my_function(&in t_variable);
    ...
    }


    /*my function definition*/
    my_function(int i[])
    {
    ......

    }

    my question is why doesnt the compiler recognize it as an empty array declaration and throw an error?? I have always assumed that the arguments in the definition of any unction are nothing but the declarations of its local objects.
    Please let me know if the question is not clear.

    Thanks
    Swapna
    What kind of error does it throw?

    Comment

    • swapnaoe
      New Member
      • Jul 2006
      • 21

      #3
      thats the worry.. the compiler doesnt throw any error. my question is why doesnt it throw any error.

      main()
      {
      int a[];

      }
      throws an error empty array declaration whereas the same thing declared in function definition as part of its arguments doesnt throw any such error!!




      Originally posted by Savage
      What kind of error does it throw?

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Originally posted by swapnaoe
        thats the worry.. the compiler doesnt throw any error. my question is why doesnt it throw any error.

        main()
        {
        int a[];

        }
        throws an error empty array declaration whereas the same thing declared in function definition as part of its arguments doesnt throw any such error!!
        When you declare it as the function argument,it doesn't allocate the array it allocates at function call time a simple pointer(which points to the name of the first element of the array),and all pointers are of the same size,and this parameter can be passed just as:

        [CODE=c]foo(int *pArray);[/CODE]

        Note:When passing matrices (e.g) you need explicitly to specify number of columns (it needs to know how many columns to pass to get to the new row)

        [CODE=c]foo(int matrix[][COLUMNS]);

        //or

        foo(int (*matrix)[COLUMNS]);[/CODE]

        Note the parenthesis around the pointer,it's not the same, difference is:

        [CODE=c] int (*a)[35];//declares a pointer to an array of 35 ints.( () has a
        //higher precedence then [])
        int *a[35]; //declares an array of 35 pointers to ints.[/CODE]

        Comment

        Working...