C Traps: Finding array Lengths

Source: Internet
Author: User
Tags array length

This is an imported old blog, may have a timeliness problem.

program, when we set up an array of type int:
int a[]={1,2,3,4,5,6};
We may then need to know its length, which can be used in this way:
length = sizeof (a)/sizeof (a[0]);

This method is useful, but can you use a custom function to receive an array as a parameter, to find its length?
Intuitively, we might write a program like this:

#include <stdio.h>int len (int a[]) {int len = sizeof (a)/sizeof (a[0]); return Len; int main () {int a[]={11,12,13,14,15,16};p rintf ("%d\n", Len (a)); return 0;}
But soon we will find that the result is wrong. Careful analysis will find that in the Len (int a[]) function, the result of sizeof (a) is actually the length of the A pointer (length in my system is 8), not the length of the entire array A.
This is because, when passing an array as a parameter, the array is degraded to the pointer, so the array length is no longer available

I saw it on the Internet. Using a pointer method can be done in the function to find the length of the array parameters, I put his code slightly modified for easy observation debugging, as follows:

#include <stdio.h>int len (int a[]) {int len = 0;int *p;p = A; while ((*p)!=null) {printf ("Current pointer:%d\n", p);p RI NTF ("Current Value:%d\n", *p);p ++;len++;} printf ("Current pointer:%d\n", p);p the rintf ("Current Value:%d\n\n", *p); return Len;}        int main () {int a[]={11,12,13,14,15,16}; printf ("Actual length:%d\n\n", sizeof (a)/sizeof (A[0]));p rintf ("Length:%d\n", Len (a)); return 0;}
In my system, one of the running results is this:


To analyze the program and the results, we first know that this method is wrong (the result is sometimes correct and sometimes wrong, and the result of the error in the diagram).
Second, we see that when the pointer P is self-adding, p does point to the address of the next element honestly (in this case, the address is added by 4, or int length).
Finally, the procedure uses (*P)!=null as the judging condition, judging whether the array range has been exceeded, This operation is dangerous because it produces an array out of bounds, and this is not a warning in the C language。 The program accesses the unpredictable address content across the bounds, obtains an unpredictable value of 1 at 10485320, and counts it as an element in the array. At the next address, which is 10485324, the value is 0, which is considered null and the array ends.

Conclusion: In C, the array is passed as an argument, and the function that receives the array cannot determine the length of the array. The workaround is to pass the array to the length of the array as a separate argument, or the array length as a global variable, so that the passed array is available.

C Traps: Finding array Lengths

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.