Initialization of an array of pointers

Source: Internet
Author: User

Consider the question of writing a function, Month_name (n), which returns a pointer to a string of nth-month names. This is an ideal application for an array of internal static types. The Month_name function contains a private array of strings that, when called, returns a pointer to the correct element.

The initialization syntax for a pointer array is similar to the initialization syntax for the other types of objects described earlier:

Char*month_name (intN) {    Static Char*name[]={        "Illegal month",        "January","February","March",        "April"," May","June",        "July","August","September",        "October","November","December"    }; return(n<1|| N> A)? name[0]:name[n];}

Where the declaration of name is the same as the declaration of lineptr in the sort example, is a one-dimensional array, and the elements of the array are character pointers. The initialization of the name array is implemented by a string list that assigns each string in the list to the element at the corresponding position in the array. All characters of the I string are stored in a location in memory, and pointers to it are stored in name[i]. Since the length of the array name is not specified in the above declaration, the compiler compiles the number of initial values and fills the exact number into the length of the array.

Initialization of an array of pointers

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.