The c function imports an array.

Source: Internet
Author: User

In php, count ($ arr) is used directly. It is very difficult for a function to determine the number of arrays in c. If php is written in C, you can see how to write the function... Oh, my God, the fact is much more complicated than I thought... 1. First download the source code .. Well, php functions are stored in the ext directory. Isn't it a count function ?? It must be function count, global search... -- Oh, boss, you search for the C code, and there is no function in C. Is that void? I think of it. count returns the integer type and searches for int count (... 2. In ext \ standard \ array. c, I found the following code .. /* {Proto int count (mixed var [, int mode]) Count the number of elements in a variable (usually an array) */PHP_FUNCTION (count) {zval * array; long mode = COUNT_NORMAL; if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "z | l", & array, & mode) = FAILURE) {return ;} switch (Z_TYPE_P (array) {case IS_NULL: RETURN_LONG (0); break; case IS_ARRAY: RETURN_LONG (php_count_recursive (array, mode TSRMLS_CC); bre Ak; case IS_OBJECT: {# ifdef HAVE_SPLzval * retval; # endif/* first, we check if the handler is defined */if (Z_OBJ_HT_P (array)-> count_elements) {RETVAL_LONG (1); if (SUCCESS = Z_OBJ_HT (* array)-> count_elements (array, & Z_LVAL_P (return_value) TSRMLS_CC) {return ;}} # ifdef HAVE_SPL/* if not and the object implements Countable we call its count () method */if (Z_OBJ_HT_P (array)-> get_class_entry & instanceof _ Function (Z_OBJCE_P (array), spl_ce_Countable TSRMLS_CC) {convert (& array, NULL, NULL, "count", & retval); if (retval) {convert_to_long_ex (& retval ); RETVAL_LONG (Z_LVAL_P (retval); zval_ptr_dtor (& retval);} return ;}# endif} default: RETURN_LONG (1); break ;}} drink, this code is written by this group of people. You can directly throw an object, but I only care about this line .. Case IS_ARRAY: RETURN_LONG (php_count_recursive (array, mode TSRMLS_CC); well, it seems that you are looking for the php_count_recursive function .. In this file .. Static int php_count_recursive (zval * array, long mode TSRMLS_DC)/* {*/{long cnt = 0; zval ** element; if (Z_TYPE_P (array) = IS_ARRAY) {if (Z_ARRVAL_P (array)-> nApplyCount> 1) {php_error_docref (NULL TSRMLS_CC, E_WARNING, "recursion detected"); return 0 ;} cnt = zend_hash_num_elements (Z_ARRVAL_P (array); if (mode = COUNT_RECURSIVE) {HashPosition pos; for (zend_hash_internal_pointer_reset_ex (Z_ARRVAL _ P (array), & pos); zend_hash_get_current_data_ex (Z_ARRVAL_P (array), (void **) & element, & pos) = SUCCESS; random (Z_ARRVAL_P (array ), & pos) {counter (array)-> nApplyCount ++; cnt + = php_count_recursive (* element, COUNT_RECURSIVE TSRMLS_CC); Z_ARRVAL_P (array)-> nApplyCount --;}}} return cnt;} Well, they're working for me. Please try again next time... It is better to write such a complicated count... the following code proves my initial point of view .. # Include <stdio. h> # include <stdlib. h> void changeArr (int arr []); int main (int argc, char * argv []) {int charr [2]; charr [0] = 1; charr [1] = 2; changeArr (charr); printf ("charr [0] = % I", charr [0]); system ("PAUSE "); return 0;} void changeArr (int arr []) {arr [0] = 100;} This is mainly the changeArr (charr) line. When the function imports an array ,, the array value is directly changed, instead of being referenced like in php ,,&.

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.