#include <stdio.h> #include <stdlib.h> #include <string.h> const size_t BUFFER_LEN =128; const size_t NUM_P =3; char buffer[128]; char *pS[3]={NULL}; char *pbuffer =buffer ; int i; int main() { printf("\n You can enter up to %u message each up to %u ge ",NUM_P,BUFFER_LEN-1); for (i=0;i<NUM_P;i++) { pbuffer=buffer ; printf("\n Enter %s message ",i>0?"another":"a"); while ((pbuffer-buffer<BUFFER_LEN-1)&&(*(pbuffer++)=getchar())!=‘\n‘); if((pbuffer-buffer)<2) break; if((pbuffer-buffer)==BUFFER_LEN&&*(pbuffer-1)!=‘\n‘) { printf("String too long "); i--; continue; } *(pbuffer-1)=‘\0‘; pS[i]=(char*)malloc(pbuffer-buffer); if(pS[i]==NULL) { printf("\n OUT of memory "); return 1; } strcpy(pS[i],buffer); } printf("\n In reverse order .the strings you entered are :\n"); while (--i >= 0) { printf("\n%s",pS[i]); free(pS[i]); pS[i]=NULL; } system("pause"); return 0; }
时间: 2024-11-05 12:21:40