473,382 Members | 1,726 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,382 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 24180
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...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.