求助各位高手,关于指针控制字符串的一道题
#include<stdio.h>#include<stdlib.h>
const size_t BUFFER_LEN =10;
int main(void)
{
char buffer[BUFFER_LEN];
char *pS[3]={NULL};
char *pbuffer =buffer;
size_t index=0;
printf("\nEnter 3 message that total less than %u characters.",BUFFER_LEN-3);
for(int i=0;i<3;i++)
{
printf("Enter %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.");
system("pause");
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);
system("pause");
return 0;
}
问题一:当输入“111111111”回车,显示空间不足,与 if((index==BUFFER_LEN)&&((*(pbuffer+index-1)!='\0')||(i<2)))貌似不符。
问题二:if((index==BUFFER_LEN)&&((*(pbuffer+index-1)!='\0')||(i<2))) 中i<2加不加是不是无所谓的