473,325 Members | 2,671 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,325 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 6323
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.