Two questions that come to mind as multi-dimensional arrays of function parameters

Source: Internet
Author: User

The multi-dimensional array as a function parameter is described in C and pointer:

The multi-dimensional array names used as function parameters are passed in the same way as the one-dimensional array names-actually passing a pointer to the first element of the array. However, the difference between the two is that each element of a multi-dimensional array itself is another array, and the compiler needs to know its dimension to evaluate the subscript expression of a function parameter.

In this article, there is no objection to the general int-type two-dimensional array, but the situation for the string array seems a bit different. The following is an example:

# Include <unistd. h> # include <stdio. h> int main (void) {char * choise [] = {"test choise 1", "this is test choise 2", null}; print (choise );} int print (char ** PS) {char ** pp = Ps; while (* PP) {printf ("% s \ n", * PP ); PP ++ ;}}

If this program follows the rules described above, when executing the print function pp ++, the program should not run normally. The result is that the program can print the string correctly.

For a string array, It is a character-shaped two-dimensional array, but it also has its own particularity, that is, the string is an array of characters ending with '\ 0. That is to say, each sub-array of the Two-dimensional array choise ends with '\ 0'. In this way, we can tell the compiler parameter PS dimension. The form parameter of the print function is equivalent to Char (* PS) [22].

In this program, there is also a small problem, that is, the difference between null and. Null is nothing, that is, 0. "" Is an empty string, and the empty string is also a string. It only has one character '\ 0 '.

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.