zoukankan      html  css  js  c++  java
  • c 函数传入数组。

    php 里面 直接 count($arr); 一个函数搞定,
    c里面想判断下数组的个数却非常困难。
    想到php是C写的,那看看他的函数怎么写的不就行了。。。
    哦,天啊,,事实比我想的要复杂的多。。。
    1,首先源码下载下来了。。嗯,php函数都是放ext目录下的。不就一个count函数么??肯定是function count,全局搜索吧。。。--哦,老大,你搜的是C代码,C里可没有function   。那是void? 我想起来了,count返回整数类型,搜 int count(..
    2,在 extstandardarray.c 里面我找到了以下代码。。
    /* {{{ 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));
    break;
    case IS_OBJECT: {
    #ifdef HAVE_SPL
    zval *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)) {
    zend_call_method_with_0_params(&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;
    }
    }
     
    喝,这群人这代码写的,你可以直接扔一个对象进去,,不过我只在乎这行。。
    case IS_ARRAY:
    RETURN_LONG (php_count_recursive (array, mode TSRMLS_CC));
     
    嗯,看来要找php_count_recursive这个函数了。。
    就在本文件里。。
    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;
    zend_hash_move_forward_ex(Z_ARRVAL_P(array), &pos)
    ) {
    Z_ARRVAL_P(array)->nApplyCount++;
    cnt += php_count_recursive(*element, COUNT_RECURSIVE TSRMLS_CC);
    Z_ARRVAL_P(array)->nApplyCount--;
    }
    }
    }
     
    return cnt;
    }
     
    嗯,他们找我干活了,下次再找吧。。。这么复杂的count还不如我自己写个了........
     
    以下代码证明了我初始想验证的观点。。
    #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;
    }
    主要是这行  changeArr(charr);  当函数传入数组时,,数组值被直接改变,,而不用像php中要用引用,,&。
  • 相关阅读:
    Android学习小Demo(20)关于Fragment的应用
    计算机常识--框架、编程语言篇
    用python做oj上的简单题(持续更新中.......)
    UIControl-IOS开发
    Android各种屏幕分辨率(VGA、HVGA、QVGA、WQVGA、WVGA、FWVGA) 具体解释
    包管理器Bower使用手冊之中的一个
    23web app实现上下左右滑动
    iOS IAP教程
    一步一步写算法(之 A*算法)
    执行游戏时出现0xc000007b错误的解决方法
  • 原文地址:https://www.cnblogs.com/guanliyang/p/3622806.html
Copyright © 2011-2022 走看看