Home > php教程 > PHP开发 > body text

The difference between string pointer and character array

高洛峰
Release: 2016-12-12 17:22:23
Original
2025 people have browsed it

Character arrays and character pointer variables can be used to store and operate strings. But there is a difference between the two. You should pay attention to the following issues when using it:

1. The string pointer variable itself is a variable used to store the first address of the string. The string itself is stored in a continuous memory space headed by the first address and ends with ‘

View Code  
  #include<iostream.h>  
 #include<ctype.h>  
    
   /******************************************************************************/  
  /* 
   *    Convert a string to lower case 
   */  
    
  int strlower(char *string)  
 {  
     if(string==NULL)  
     {  
         return -1;  
     }  
   
     while(*string)  
     {  
         if(isupper(*string))  
             *string=tolower(*string);  
         string++;  
     }  
     *string=&#39;\0&#39;;  
     return 0;  
 }  
 /*char *strlower(char *string) 
 { 
     char    *s; 
  
      
  
     if (string == NULL) { 
         return NULL; 
     } 
  
     s = string; 
     while (*s) { 
         if (isupper(*s)) { 
             *s = (char) tolower(*s); 
         } 
         s++; 
     } 
     *s = &#39;\0&#39;; 
     return string; 
 } 
 */  
   
 void main()  
 {  
     char *test="ABCDEFGhijklmN";  
     strlower(test);  
     cout<<test<<endl;  
 }
Copy after login

其中,如果采用char *test=”ABCDEFGhijklmN”;会产生运行时错误。Char test[]=”ABCDEFGhijklmN”则程序正常运行,原因如前所述。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!