473,225 Members | 1,318 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

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 6287
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

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

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
3
by: Goh, Yong Kwang | last post by:
I'm trying to create a function that given a string, tokenize it and put into a dynamically-sized array of char* which is in turn also dynamically allocated based on the string token length. I...
5
by: Tee | last post by:
Hi, When I have a function like: private string xyz(string Text) I can call it without passing in a string with xyz("") or xyz(string.Empty) but if the paramater needed is an array like:...
59
by: Steve R. Hastings | last post by:
So, Python 2.5 will have new any() and all() functions. http://www.python.org/dev/peps/pep-0356/ any(seq) returns True if any value in seq evaluates true, False otherwise. all(seq) returns...
3
by: assgar | last post by:
Hi I thought I had resolved this problem but I am intermittently the receving the warnings below. This code consist of a 1) HTML form, 2) a display function on the HTML form and 3) a...
4
by: assgar | last post by:
Hi Seasons Greetings Its back, I am being haunted. I thought I had resolved this problem but I am intermittently the receving the warnings below. This code consist of a 1) HTML form, 2)...
7
by: bowlderster | last post by:
Hello,all. I want to get the array size in a function, and the array is an argument of the function. I try the following code. /*************************************** */ #include<stdio.h>...
2
by: flydev | last post by:
Hello, I have very little knowledge when it comes to SOAP, but a developer I'm working with says it will make things easier when passing data to/from client software and the mySQL database. Anyhow, I...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.