473,385 Members | 1,732 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,385 software developers and data experts.

C Array length function problem

2
I've been experimenting with arrays, and I have just found the simple way to find the length of an array. I attempted to make this a function with the specific array as the only argument. It didn't work. I thought it was bad coding; but, after a while, I got fed up and put the same code in both the main() and the function. Like so
Expand|Select|Wrap|Line Numbers
  1. int getLength(int *myarray);
  2. int main(void)
  3. {
  4.     int myarray[]={5,4,3,2,1};
  5.     int size1=(sizeof(myarray) / sizeof(myarray[0]));
  6.     int size2=int_getLength(myarray);
  7.     printf("%d and %d should be the same",size1,size2);
  8.     return 0;
  9. }
  10. int getLength(int *myarray)
  11. {
  12.     return (sizeof(myarray) / sizeof(myarray[0]));
  13. }
  14.  
size1,main, prints to be 5, which is the correct length.

size2, function, prints 1.

I don't know why this is happening. I have also tried using (int myarray[])as the parameter, but the output is the same.

My logical assumption would be that the function is only passing in the first element of the array or it could do something with obtaining the length. Can someone please explain why this is happening.
Feb 9 '08 #1
3 24181
gpraghuram
1,275 Expert 1GB
I've been experimenting with arrays, and I have just found the simple way to find the length of an array. I attempted to make this a function with the specific array as the only argument. It didn't work. I thought it was bad coding; but, after a while, I got fed up and put the same code in both the main() and the function. Like so
Expand|Select|Wrap|Line Numbers
  1. int getLength(int *myarray);
  2. int main(void)
  3. {
  4.     int myarray[]={5,4,3,2,1};
  5.     int size1=(sizeof(myarray) / sizeof(myarray[0]));
  6.     int size2=int_getLength(myarray);
  7.     printf("%d and %d should be the same",size1,size2);
  8.     return 0;
  9. }
  10. int getLength(int *myarray)
  11. {
  12.     return (sizeof(myarray) / sizeof(myarray[0]));
  13. }
  14.  
size1,main, prints to be 5, which is the correct length.

size2, function, prints 1.

I don't know why this is happening. I have also tried using (int myarray[])as the parameter, but the output is the same.

My logical assumption would be that the function is only passing in the first element of the array or it could do something with obtaining the length. Can someone please explain why this is happening.
When single dimension array is passed as a parameter to a function then it is passed as a pointer.
Thats why u are getting the size like that
Raghuram
Feb 9 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
The real problem is a thing called decay of array.

That is, when an array is passed to a function, all that is passed is the address of element 0. So the function gets and address and not the entire array. From the perspective of the function, the array has disappeared and all the function sees is a pointee to a single vaiable. The function must assume there is an array.

Then, the sizeof only tells you the size of the variable on the local stack. In this case, the size of the address, which is most likely 4.

So, don't use that sizeof trick to get the number of array elements. Instead define a const int that is set to the number of elements and pass that along withe the array name to the function.
Feb 9 '08 #3
2n2is5
2
The real problem is a thing called decay of array.

That is, when an array is passed to a function, all that is passed is the address of element 0. So the function gets and address and not the entire array. From the perspective of the function, the array has disappeared and all the function sees is a pointee to a single vaiable. The function must assume there is an array.

Then, the sizeof only tells you the size of the variable on the local stack. In this case, the size of the address, which is most likely 4.

So, don't use that sizeof trick to get the number of array elements. Instead define a const int that is set to the number of elements and pass that along withe the array name to the function.
I had assumed that was the only way, Thanks.
Feb 9 '08 #4

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

Similar topics

1
by: jon wayne | last post by:
Hi Am trying to replace the classic switch case construct (am workng on a telecom stack devlpmnt)with an array of function ptrs. The problm am facing is that of indexing. - the case vals can be...
4
by: Isaac | last post by:
Hi mates I want to know a simple program of return array from function ? Do I need to use pointer to return the address of the first element in an array. Isaac
15
by: fdunne2 | last post by:
The following C-code implements a simple FIR filter: //realtime filter demo #include <stdio.h> #include <stdlib.h> //function defination float rtFilter1(float *num, float *den, float...
1
by: Samuel R. Neff | last post by:
Are there any differences between using Array.Length and Array.GetUpperBound(0) on a one-dimensional array? We have a team of developers and most people use Array.Length but one developer uses...
3
by: Madhu | last post by:
I would like to know how the following will be handled in the .net framework: Pl. don't pay attention to the syntax int a int b a=2 a=2 a=2
2
by: Garg | last post by:
if we assign array.length = 0 then what does it mean? Are we setting the length of the array equal to 0 or are we selecting the first element of the array? Please help me on this..
5
by: viperdriver87 | last post by:
Greetings all, I had another post on here about a Dynamic array of ints problem, and with your help, I managed to solve that problem. Now, I've another issue. I need to dyanically allocate an array...
3
by: Steve | last post by:
I am having problems with this piece of script. Usually the script works and the alert gives an array length of 9. But every so often the array length is 8 and naturally the output is then wrong....
1
by: lenniekuah | last post by:
Hullo Wonderful Friends I need your help, Please help me. I am using C#NET2008 to create Window Application using ARRAY. and ARRAY LENGTH I do encounter a very surprising error with ARRAY...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.