468,484 Members | 2,076 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

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(&int_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
Jan 13 '08 #1
3 4445
Savage
1,764 Expert 1GB
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(&int_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?
Jan 13 '08 #2
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!!




What kind of error does it throw?
Jan 14 '08 #3
Savage
1,764 Expert 1GB
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:

Expand|Select|Wrap|Line Numbers
  1. foo(int *pArray);
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)

Expand|Select|Wrap|Line Numbers
  1. foo(int matrix[][COLUMNS]);
  2.  
  3. //or
  4.  
  5. foo(int (*matrix)[COLUMNS]);
Note the parenthesis around the pointer,it's not the same, difference is:

Expand|Select|Wrap|Line Numbers
  1.    int (*a)[35];//declares a pointer to an array of 35 ints.( () has a     
  2.              //higher precedence then [])
  3. int *a[35]; //declares an array of 35 pointers to ints.
Jan 14 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Wayno | last post: by
3 posts views Thread by tornado | last post: by
59 posts views Thread by Steve R. Hastings | last post: by
3 posts views Thread by assgar | last post: by
4 posts views Thread by assgar | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.