指针数组输出问题
输入三个字符串在最后打印出来#include <stdio.h>
const size_t BUFFER_LEN = 512; //数组buffer长度
int main(void)
{
char buffer[BUFFER_LEN]; //存字符串
char *ps[3] = { NULL }; //存储数组BUFFER中字符串的地址
char *p = buffer; //遍历数组BUFFER
size_t index = 0; //记录数组BUFFER中当前未用的元素位置
for(int i = 0; i < 3; i++){
printf("\nEnter %s messgaes\n", i>0 ? "another" : "a");
ps[i] = &buffer[index]; //保存每个字符串开始位置的地址
/*读取字符串并添加终止符*/
while(index < BUFFER_LEN){
if((*(p+index) = getchar()) == '\n'){
*(p+index++) = '\0';
break;
}
/*检查数组容量是否满*/
if((index == BUFFER_LEN) && ((*(p+index-1) != '\0') || i<2)){
printf("\nYou ran out of space in the buffer,");
return 1;
}
index++;
}
}
/*打印输入的字符串*/
printf("\nThe strings you entered are:\n");
for(int i = 0; i < 3; i++)
printf("%s\n", ps[i]); //这里为什么用ps[i]而不是*ps[i]
return 0;
}