C Language Basics Tutorial (iv) Pointers, structures, unions, and enumerations (8)

Source: Internet
Author: User
Tags arrays definition constant month name variables
4. Array of pointers
Because pointers are variables, it is conceivable that pointers to the same data type can be used to form an array, which is an array of pointers. Each element in the array is a pointer variable, and each element in the pointer array is a pointer to the same data type, depending on the definition of the array. The array of pointers is defined in the following format:
Type identity * array name [integer constant expression];
For example:
int *a[10];
Defines an array of pointers, each of which is a pointer to an integer, consisting of 10 elements, a[0],a[1],a[2], ..., a[9], all of which are pointer variables. A is the name of the array of pointers, and arrays, a is a constant and cannot be incrementally calculated. A is the address of the pointer array element a[0], A+i is the address of a[i], *a is a[0],* (a+i) is a[i.
Why do you want to define and use an array of pointers? The main reason is that the array of pointers provides greater convenience and flexibility for processing strings, and that using two-dimensional arrays is inefficient for handling body lengths, while pointer arrays are convenient for manipulating body rows through address operations because each element is a pointer variable.
An array of pointers, like a generic array, allows an array of pointers to be initialized at definition, but because each element of the pointer array is a pointer variable, it can only hold an address, so the pointer array pointing to a string assigns the first address of the string to the corresponding element of the pointer array, For example, here is a writing function, Month_name (n), which returns a pointer to a character containing the nth month name (about the function, which is specifically described in section 6th).
Example 2: Print the month name from January to December:
char *month_name (int n)
{
Static Char *name[]={
"Illegal Month",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
Return (n<1| | n>12)? Name[0]:name[n]);
}
Main ()
{
int i;
for (i=0; i<13; i++)
printf ("%s\n", Month_name (i));
Related Article

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.