TaoUp0x7e0 2017-04-15 03:09 采纳率: 100%
浏览 812
已采纳

c里关于char指针的问题求解答

求解答下面c代码片段中为什么语句: if(non_alphanum(*(++ch)))
中使用*(++ch)不行,必须使用*(ch+1)?
补充环境信息:Linux 4.4.0-64-generic #85-Ubuntu

 #define is_alphanum(c) (((c)>='A' && (c) <= 'Z') || ((c) >= 'a' && \
        (c) <= 'z') || ((c) >= '0' && (c) <= '9'))
#define non_alphanum(c) !is_alphanum((c))

int is_word_delimiter(char * ch)
{
    /*
     * Use a static variable to remember whether previous 
     * character is a delimiter, if true, then the 
     * following '\'' is also a delimiter
     * e.g. "hello 'world'":both '\'' is delimiter
     *  "don't": '\'' is not a delimiter
     *
     *  Initialize the variable to 1 to indicate "'hello'" cases
     */
    static int pre_char_is_delimiter = 1;
    if(*ch == '\''){
        /*
         * NOTE: can't use *(++ch), dont know why 
         * but is truely has the side effect of 
         * changing the ch pointer itself.
         * Gdb shows that ++ch cause ch pointer to 
         * shift 3 characters, WTK.
         */
        if(non_alphanum(*(++ch))){
            pre_char_is_delimiter = 1;
            return 1;
        }else if(pre_char_is_delimiter){
            pre_char_is_delimiter = 1;
            return 1;
        }else{
            pre_char_is_delimiter = 0;
            return 0;
        }
    }   
    if(is_alphanum(*ch)){
        pre_char_is_delimiter = 0;
        return 0;
    } else{
        pre_char_is_delimiter = 1;
        return 1;
    }
}

  • 写回答

2条回答 默认 最新

  • 战在春秋 2017-04-15 08:20
    关注

    #define non_alphanum(c) !is_alphanum((c))

    定义了宏non_alphanum,
    这样

     if(non_alphanum(*(++ch)))
    

    相当于

     if(!is_alphanum((*(++ch))))
    

    而is_alphanum又是一个宏, 具体你可以看下代码头部

    这样在is_alphanum宏展开后,ch自增了不止一次,从而出现了题目中的现象。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划