user-0 提问: 字符串逆序函数，运行段错误，求解释！

回复:
user: user-1
content: 末尾最好释放ch,虽然程序结束就自动回收了。但这是个好习惯。

user: user-1
content: #include <stdio.h> #include <string.h> #include <malloc.h>  #define BUFSIZE 100 char* reverse(char *str) {     int n = strlen(str);     char *temp = (char *) malloc(sizeof(str));     char *ch = temp;      while (*str) { 	str++;     }     str--;      while (n--) {         *(temp++) = *(str--);     }      return ch;  }  int main(int argc, char **argv) {     char str[BUFSIZE];      char *ch = NULL;     printf("enter:\n");     scanf("%s", &str);     /*      int n = strlen(str);      *      char temp;      *      int  i = 0;      *      *      for(i=0; i<n/2; i++){      *      temp = str[n-1-i];      *      str[n-1-i] = str[i];      *      str[i] = temp;      *      }      *      printf("%s\n",str);     */     ch = reverse(str);     printf("%s\n", ch);      return 0; }

user: user-2
content: 修改后如下，第一申请动态内存就要释放。第二字符串要一'\0'结束，第三你str是个野指针，要先指向字符数组  #include <stdio.h> #include <string.h> #include <malloc.h>  char *reverse(char *str) { int n = strlen(str); char *temp = (char *)malloc(strlen(str)+1); char *ch = temp;  while(*str){ str++; } str--;  while(n--){  *temp++ = *str--; } *temp='\0'; return ch;  }  int main(int argc, char **argv) { char *str; char str1[50]; str=str1;  char *ch = NULL; printf("enter:\n"); scanf("%s",str); ch = reverse(str); printf("%s\n",ch); free(ch);  return 0; }

user: user-3
content: char *temp = (char *)malloc(sizeof(str)); 改为 char *temp = (char *)malloc(n+1); temp[n] = 0;

user: user-4
content: C:\Program Files\Microsoft Visual Studio\VC98\CRT\SRC\STRREV.C /*** *strrev.c - reverse a string in place * *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved. * *Purpose: *       defines _strrev() - reverse a string in place (not including *       '\0' character) * *******************************************************************************/  #include <cruntime.h> #include <string.h>  /*** *char *_strrev(string) - reverse a string in place * *Purpose: *       Reverses the order of characters in the string.  The terminating *       null character remains in place. * *Entry: *       char *string - string to reverse * *Exit: *       returns string - now with reversed characters * *Exceptions: * *******************************************************************************/  char * __cdecl _strrev (         char * string         ) {         char *start = string;         char *left = string;         char ch;          while (*string++)                 /* find end of string */                 ;         string -= 2;          while (left < string)         {                 ch = *left;                 *left++ = *string;                 *string-- = ch;         }          return(start); }   C:\Program Files\Microsoft Visual Studio\VC98\CRT\SRC\WCSREV.C /*** *wcsrev.c - reverse a wide-character string in place * *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved. * *Purpose: *       defines _wcsrev() - reverse a wchar_t string in place (not including *       L'\0' character) * *******************************************************************************/   #include <cruntime.h> #include <string.h>  /*** *wchar_t *_wcsrev(string) - reverse a wide-character string in place * *Purpose: *       Reverses the order of characters in the string.  The terminating *       null character remains in place (wide-characters). * *Entry: *       wchar_t *string - string to reverse * *Exit: *       returns string - now with reversed characters * *Exceptions: * *******************************************************************************/  wchar_t * __cdecl _wcsrev (         wchar_t * string         ) {         wchar_t *start = string;         wchar_t *left = string;         wchar_t ch;          while (*string++)                 /* find end of string */                 ;         string -= 2;          while (left < string)         {                 ch = *left;                 *left++ = *string;                 *string-- = ch;         }          return(start); }    C:\Program Files\Microsoft Visual Studio\VC98\CRT\SRC\MBSREV.C /*** *mbsrev.c - Reverse a string in place (MBCS) * *       Copyright (c) 1985-1997, Microsoft Corporation.  All rights reserved. * *Purpose: *       Reverse a string in place (MBCS) * *******************************************************************************/  #ifdef _MBCS  #include <mtdll.h> #include <cruntime.h> #include <string.h> #include <mbdata.h> #include <mbctype.h> #include <mbstring.h>   /*** * _mbsrev - Reverse a string in place (MBCS) * *Purpose: *       Reverses the order of characters in the string.  The terminating *       null character remains in place.  The order of MBCS characters *       is not changed. * *Entry: *       unsigned char *string = string to reverse * *Exit: *       returns string - now with reversed characters * *Exceptions: * *******************************************************************************/  unsigned char * __cdecl _mbsrev(         unsigned char *string         ) {          unsigned char *start = string;         unsigned char *left  = string;         unsigned char c;          if ( _ISNOTMBCP )                 return _strrev(string);          _mlock(_MB_CP_LOCK);          /* first go through and reverse the bytes in MBCS chars */         while ( *string ) {                  if ( _ISLEADBYTE(*string++) ) {                         if ( *string ) {                                 c = *string;                                 *string = *(string - 1);                                 *(string - 1) = c;                                 string++;                         }                         else /* second byte is EOS */                                 break;                 }         }          /* now reverse the whole string */         string--;         while ( left < string ) {                 c = *left;                 *left++ = *string;                 *string-- = c;         }          _munlock(_MB_CP_LOCK);         return ( start ); }  #endif  /* _MBCS */

user: user-5
content: 一个简单的问题，用指针指来指去，很容易弄错额， 指针这玩意就是一把双刃剑， # include <stdio.h> # include <string.h>  void reverse(char *Dest, const char *Source);  int main() { 	char s1[20], s2[20];  	printf("input:"); 	scanf("%s", s1); 	 	reverse(s2, s1); 	printf("reverse:%s\n", s2);  	return 0; }  void reverse(char *Dest, const char *Source) { 	int i, n; 	if (Dest != NULL && Source != NULL) 	{ 		n = strlen(Source); 		Dest[n] = '\0';  		for (i = 0; i < n; i++) 			Dest[i] = Source[n - i - 1];  	} }