Assignment of a C-language two-dimensional array string

Source: Internet
Author: User
Tags sprintf

Today, two-dimensional arrays are used as parameters to pass the program, through the Internet search, for their own problems to do a collation.

1, in the called function of the shape parameter group definition can omit the size of the first dimension, the second dimension cannot be omitted, as follows:

void fun (int a[5][20]); Legal

void fun (int a[][20]); Legal

void fun (int a[5][]); Not legal

void fun (int a[][]); Not legal

When a two-dimensional array as a formal parameter, the first dimension can be omitted, the second dimension cannot be omitted, this is the compiler principle is limited, in memory by the array rules are stored (in rows), but not the branch and column, if the parameter does not describe the number of columns, the system cannot determine how many rows should be.

2, the real parameter group dimension can be greater than the shape parameter group dimension, such as:

void fun (int a[5][20]);//formal parameter is a[5][20]

int a[10][20]; Defining Real parameter Groups

The parameter group is then only part of the data of the real parameter group.

3. String two-dimensional array assignment

3.1. Initialize the value of the assignment

void Fun (char a[][20]) {//implement function function} void Main () {char str[][20]={"123", "456", "789"};    Fun (str); }

3.2, use strcpy function, need #include<string.h>

void Fun (char a[][20]) {//implement function function} void Main () {char str[3][20];       strcpy (str[0], "123");//or with sprintf (str[0], "123");       strcpy (Str[1], "456");//or with sprintf (str[1], "456");       strcpy (str[2], "789");//or with sprintf (str[2], "789");    Fun (str); }


Assignment of a C-language two-dimensional array string

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.