新闻中心

EEPW首页 > 嵌入式系统 > 牛人业话 > C语言的那些小秘密之指针(三)

C语言的那些小秘密之指针(三)

作者:时间:2015-03-31来源:网络收藏

  但凡人都是急功近利和有惰性的,我就是个例子。不想每篇博客都写一个摘要和那些大段的文字描述,但是为了让一些新的读者朋友了解我的博客内容,我还是像前面说的那样,把第一篇关于的摘要搬过来,因为我写的这几篇都是关于的,所以没有必要每篇一个摘要,在此就偷偷懒了,如果读过我前面两篇C博客的朋友可以跳过这篇博客前面的摘要不读,直接进入主题部分。

本文引用地址:http://www.eepw.com.cn/article/271867.htm

  懂得的人都知道,之所以强大,以及其自由性,绝大部分体现在其灵活的指针运用上。因此,说指针是c语言的灵魂,一点都不为过。所以从我的标题加了个(一)也可以看出指针的重要性,我尽可能的向大家交代清楚我对于指针的理解。所以在讲解的过程中我尽可能的用代码加文字的描述方式,通过代码的分析来加深我们对于指针的理解,我给出的都是完整的代码,所以读者可以在看的过程中直接copy下去即可运行,希望下面的讲解能够对你有所帮助。

  在中我们可以使用两种方法来访问一个字符串。

  1、用字符数组存放一个字符串

  char str[ ]="this is str!!!";

  在此str是一个数组名,代表字符串数组的首地址。

  2、用字符指针指向一个字符串

  char *str="this is str,too";

  C语言对于字符串常量是按照字符数组的方式来进行处理的,在内存开辟了一个字符数组来存放字符串常量。在此的str被定义为一个指针变量,指向字符型数组,它只能指向一个字符变量和其他的字符数据。输出都是使用printf("%s",str);,在此过程中系统会先输出一个str所指向的字符数据,然后使用str加1的方法输出下一个字符,知道遇到''串结束符为止,在内存中的字符串的最后都被自动加上一个''。

  字符串的传递可以使用传址的方法,用字符数组的名字或者用指向字符数组的指针变量作为参数。在被调用的函数中可以改变字符串的内容,主函数可以得到改变了的字符串。

  一、用字符数组作为参数

  接下来看看一个代码:

  #include

  void copy_string(char from[],char to[])

  {

  int i=0;

  while(from[i]!=''){

  (to[i]=from[i]);

  i++;

  }

  to[i]='';

  return ;

  }

  int main()

  {

  char str[]="this is a string!";

  printf("%sn",str);

  char dec_str[206];

  copy_string(str,dec_str);

  printf("%sn",dec_str);

  return 0;

  }

  运行结果为:

  

 

  仅仅从代码实现的难易程度来看的话没有什么难点,但是可能有不少人可能会把我们代码中用红色标志的代码部分忘掉,从而出错。

  在此也给出我的另外一种实现方式:

  #include

  void copy_string(char from[],char to[])

  {

  int i=0;

  while((to[i]=from[i++])!=''){

  ;

  }

  return ;

  }

  int main()

  {

  char str[]="this is a string!";

  printf("%sn",str);

  char dec_str[206];

  copy_string(str,dec_str);

  printf("%sn",dec_str);

  return 0;

  }

  运行结果为:

  

 

  值得注意的是这种实现方式的中的巧妙的利用了while语句while((to[i]=from[i++])!='')。千万不要写成 while((to[i++]=from[i++])!=''),这样的话每次执行判断语句是i++被执行了两次,导致最终的结果出错。

  二、用字符指针变量

  #include

  void copy_string(char *from,char *to)

  {

  int i=0;

  while(*from!='')

  *to++=*from++;

  *to='';

  return ;

  }

  int main()

  {

  char str[]="this is a string!";

  printf("%sn",str);

  char dec_str[206];

  copy_string(str,dec_str);

  printf("%sn",dec_str);

  return 0;

  }

  运行结果为:

  

 

  在此我也给出一种结合逗号表达式和for循环语句来实现的参考代码:

  #include

  void copy_string(char *from,char *to)

  {

  int i=0;

  for(;*to=*from,*from!='';from++,to++);

  return ;

  }

  int main()

  {

  char str[]="this is a string!";

  printf("%sn",str);

  char dec_str[206];

  copy_string(str,dec_str);

  printf("%sn",dec_str);

  return 0;

  }

  运行结果为:

  

 

  代码的巧妙之处在于结合了逗号表达式和for循环语句来实现,因为逗号表达式的结果为最后一个表达式的结果,所以执行判断语句里边的逗号表达式时其所取的值依然为*from!=''。

  有兴趣的读者自己可以尝试下其他的实现方法,下面再给出几种很巧妙的实现方法,有兴趣的读者可以自己研究下其实现原理,均为完整代码。

  #include

  void copy_string(char *from,char *to)

  {

  int i=0;

  for(;*to++=*from++;);

  return ;

  }

  int main()

  {

  char str[]="this is a string!";

  printf("%sn",str);

  char dec_str[206];

  copy_string(str,dec_str);

  printf("%sn",dec_str);

  return 0;

  }

  #include

  void copy_string(char *from,char *to)

  {

  int i=0;

  while(*to++=*from++);

  return ;

  }

  int main()

  {

  char str[]="this is a string!";

  printf("%sn",str);

  char dec_str[206];

  copy_string(str,dec_str);

  printf("%sn",dec_str);

  return 0;

  }

  #include

  void copy_string(char *from,char *to)

  {

  int i=0;

  while((*to++=*from++)!='');

  return ;

  }

  int main()

  {

  char str[]="this is a string!";

  printf("%sn",str);

  char dec_str[206];

  copy_string(str,dec_str);

  printf("%sn",dec_str);

  return 0;

  }

  #include

  void copy_string(char *from,char *to)

  {

  char*p1,*p2;

  p1=from;

  p2=to;

  while((*p2++=*p1++)!='');

  return ;

  }

  int main()

  {

  char str[]="this is a string!";

  printf("%sn",str);

  char dec_str[206];

  copy_string(str,dec_str);

  printf("%sn",dec_str);

  return 0;

  }

  #include

  void copy_string(char from[],char to[])

  {

  char*p1,*p2;

  p1=from;

  p2=to;

  while((*p2++=*p1++)!='');

  return ;

  }

  int main()

  {

  char str[]="this is a string!";

  printf("%sn",str);

  char dec_str[206];

  copy_string(str,dec_str);

  printf("%sn",dec_str);

  return 0;

  }

  就不在此一一贴出运行结果了,运行结果同上,如果有兴趣可以自己研究下以上代码,同时还有很多的实现方法,希望以上代码能够启发读者的灵感写出更多的实现方法和更好的实现方案来。

可控硅相关文章:可控硅工作原理


c语言相关文章:c语言教程


比较器相关文章:比较器工作原理




关键词: C语言 指针

评论


相关推荐

技术专区

关闭