Composite text in C Language

Source: Internet
Author: User

If you want to pass a value to a function with an int parameter, you can pass an int constant or an int variable. Before the C99 standard, the array parameters were different from the present, and no so-called array constants were available for transmission, while compound texts were added to C99. The text is a non-symbolic constant. For example, if 5 is an int text, 25.3 is a double text. It can be seen that if there is a text that can represent the array and structure content, programming will be more convenient.

For arrays, composite text looks like a type name enclosed by parentheses in the array initialization list. For example, the following is the declaration method of a common array:

Int a [2] = {10, 20 };

The following is a composite text, which creates a nameless array containing two int types:

(Int [2]) {10, 20 };

Note: The type name is the rest of a removed from the previous declaration, that is, int [2].

Just as the size of an array can be unspecified when initializing an array, the size of a composite text can be omitted during initialization:

(Int []) {10, 20, 30}; // composite text with three elements

Because these conforming words have no names, they cannot be created in one statement and must be used in other statements. One method is to use a pointer:

Int * p;

P = (int []) {10, 20, 30 };

Note that this literal constant is represented as an int array. Same as the array name, this constant indicates the first address of the array, as shown in this example:

* P = 10

In addition, composite text can be passed as a parameter to a function:

Int sum (int ar [], int n)

...............

Int tot;

Tot = sum (int []) {1, 2, 3, 4}, 4 );

This method transfers array information to the function without having to create an array first. It complies with the common usage of constants.

This method can also be used in the use of multi-dimensional arrays, such as creating a two-dimensional array and saving its address:

Int (* p) [4];

P = (int [2] [4]) {1, 2, 3, 4}, {5, 6, 7, 8 }};

The composite text type is int [2] [4], which is a 2*4 int array.

Comprehensive use code:

 

#include <stdio.h>#define COLS 4void sum2d (int a[][COLS],int row);void sum (int a[],int n);int main (void){int *p1;int (*p2)[COLS];p1=(int [2]){10,20};p2=(int [2][COLS]){{1,2,3,4},{5,6,7,8}};sum (p1,2);sum2d (p2,2); }void sum (int a[],int n){int tot=0;for (int i=0;i<n;i++){tot+=a[i];}printf ("The sum of all is %d\n",tot);}void sum2d (int a[][COLS],int row){int tot=0;for (int i=0;i<row;i++){for (int j=0;j<COLS;j++){tot+=a[i][j];}}printf ("The sum of all is %d\n",tot);}

 

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.