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 -
int getLength(int *myarray);
-
int main(void)
-
{
-
int myarray[]={5,4,3,2,1};
-
int size1=(sizeof(myarray) / sizeof(myarray[0]));
-
int size2=int_getLength(myarray);
-
printf("%d and %d should be the same",size1,size2);
-
return 0;
-
}
-
int getLength(int *myarray)
-
{
-
return (sizeof(myarray) / sizeof(myarray[0]));
-
}
-
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.
3 24180
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 -
int getLength(int *myarray);
-
int main(void)
-
{
-
int myarray[]={5,4,3,2,1};
-
int size1=(sizeof(myarray) / sizeof(myarray[0]));
-
int size2=int_getLength(myarray);
-
printf("%d and %d should be the same",size1,size2);
-
return 0;
-
}
-
int getLength(int *myarray)
-
{
-
return (sizeof(myarray) / sizeof(myarray[0]));
-
}
-
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
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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...
|
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...
|
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
|
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..
|
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...
|
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....
|
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...
|
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...
|
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...
|
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: 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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |