遇到难题了,Help me!!~~~我用的教材“C语言入门经典(第四版)”
我又遇到难题了,o(︶︿︶)o 唉……这个问题想了老长时间,就是想不通,可能我水平有限,真的学不动了,可是我真的很喜欢编程,喜欢坐在计算机面前,敲代码的这种感觉……我用的是“C语言入门经典 第四版” 作者lvor Horton 的书,现在学到了指针一节就看得有点吃力了、、、想在这个论坛上找些用着同样教材的同学们(或是大神们)一起学习……加QQ:963358694
言归正传:我把我的难题写出来:
源码附上:
程序代码:
/* Program 7.12 Arrays of Pointers to Strings */ #include <stdio.h> const size_t BUFFER_LEN = 512; /* Size of input buffer */ int main(void) { char buffer[BUFFER_LEN]; /* Store for strings */ char *pS[3] = { NULL }; /* Array of string pointers */ char *pbuffer = buffer; /* Pointer to buffer */ size_t index = 0; /* Available buffer position */ printf("\nEnter 3 messages that total less than %u characters.", BUFFER_LEN-2); for(int i=0 ; i<3 ; i++) { printf("\nEnter %s message\n", i>0 ? "another" : "a" ); pS[i] = &buffer[index]; for( ; index<BUFFER_LEN ; index++) if((*(pbuffer+index) = getchar()) == '\n') { *(pbuffer+index++) = '\0'; break; } if((index == BUFFER_LEN) && ((*(pbuffer+index-1) != '\0') || (i<2))) { printf("\nYou ran out of space in the buffer."); return 1; } } printf("\nThe strings you entered are:\n\n"); for(int i = 0 ; i<3 ; i++) printf("%s\n", pS[i]); printf("The buffer has %d characters unused.\n", BUFFER_LEN-index); return 0; }