What are convincing examples where pointer arithmetic is preferable to array subscripting?

前端 未结 9 616
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 09:23

I\'m preparing some slides for an introductory C class, and I\'m trying to present good examples (and motivation) for using pointer arithmetic over array subscripting.

9条回答
  •  [愿得一人]
    2020-12-30 10:13

    #include ctype.h
    void skip_spaces( const char **ppsz )
    {
      const char *psz = *ppsz;
      while( isspace(*psz) )
        psz++;
      *ppsz = psz;
    }
    
    void fn(void)
    {
      char a[]="  Hello World!";
      const char *psz = a;
      skip_spaces( &psz );
      printf("\n%s", psz);
    }
    

提交回复
热议问题