468,496 Members | 1,799 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

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 23793
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,207 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

Post your reply

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

Similar topics

4 posts views Thread by Isaac | last post: by
15 posts views Thread by fdunne2 | last post: by
3 posts views Thread by Madhu | last post: by
2 posts views Thread by Garg | last post: by
3 posts views Thread by Steve | last post: by
1 post views Thread by lenniekuah | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.