The length of the array in the parameter list of the C + + function is thought

Source: Internet
Author: User

Why does C and C + + compilers allow array lengths in function signatures when they ' re never enforced?

This is because, a long time ago (in fact, not long), the programming process found that this situation

#include<iostream>using namespaceSTD;intDis(Chara[1]){    intlength=strlen(a);    CharC=a[2];    returnlength;}intMain(){    Charb[4] = "ABC";    intC=Dis(b);cout<<C;    return 0;}  
function dis parameter a[1] After the number 1, does not work, in fact, write 2.3.4 can, this is also a lot of people know, in the spirit of inquiry to think a bit, since it does not work, then why not prohibit this behavior, still is to the StackOverflow (really is God station) Ask questions and get answers from many great gods.

Why does C and C + + compilers allow array lengths in function signatures when they ' re never enforced?

This is a better answer.

It's a quirk of the syntax for passing arrays to functions.

Actually it isn't possible to pass an array in C. If You write syntax this looks like it should pass the array and what actually happens are that a pointer to the first element Of the array is passed instead.

since The pointer does not include any length information, the contents of Your ["  in the function formal parameter list is actually ignored.

The decision to allow this syntax is made in the 1970s and have caused much confusion ever since ...

Other wonderful answers, specific can see here, will not move over.

Http://stackoverflow.com/questions/22677415/why-do-c-and-c-compilers-allow-array-lengths-in-function-signatures-when-they

The length of the array in the parameter list of the C + + function is thought

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.