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='\0'; 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 = '\0'; return string; } */ void main() { char *test="ABCDEFGhijklmN"; strlower(test); cout<<test<<endl; }
其中,如果采用char *test=”ABCDEFGhijklmN”;会产生运行时错误。Char test[]=”ABCDEFGhijklmN”则程序正常运行,原因如前所述。