一个关于神秘指针的问题,,,,小白敬上
问题都在图片里了,,,希望前辈指点,,小白敬上
程序代码:
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> bool str_in(char **); void str_sort(char *[],int ); void swap( char **p1, char **p2); void str_out(char *[],int); const size_t BUFFER_LEN = 256; const size_t NUM_P = 50; int main(void) { char *ps[NUM_P]; int count = 0; printf("\nEnter successive lines, pressing Enter at the eng of" " each line.\nJust press enderto end.\n"); for(count = 0; count<NUM_P ; count++) if(!str_in(&ps[count])) break; str_sort(ps,count); str_out(ps,count); return 0; } bool str_in(char **pString) { char buffer[BUFFER_LEN]; if(gets(buffer) == NULL) { printf("\nError reading string.\n"); exit(1); } if(buffer[0] == '\0') return false; *pString = (char *)malloc(strlen(buffer) + 1); if(*pString == NULL) { printf("\nOut of memory."); exit(1); } strcpy(*pString,buffer); return true; } void str_sort( char *p[],int n) { bool sorted = false; while(!sorted) { sorted = true; for(int i=0 ; i<n-1 ; i++) if(strcmp(p[i],p[i+1]) > 0) { sorted = false; swap(&p[i],&p[i+1]); } } } void swap(char **p1,char **p2) { char *pt = *p1; *p1 = *p2; *p2 = pt; } void str_out(char *p[], int n) { printf("\nyour input sorted in order is:\n\n"); for(int i=0 ; i<n ; i++) { printf("%s\n",p[i]); free(p[i]); p[i] = NULL; } return; }