char *pS[NUM_P]={NULL}这条代码不算初始化?
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#include<stddef.h>
const size_t BUFFER_LEN=128;
const size_t NUM_P=100; //初始化NUM_P=100
int main(void)
{
char buffer[BUFFER_LEN];
//int size_t NUM_P;
char *pS[NUM_P]={NULL} //初始化指针PS字符串=NULL(报错 variable-sized object may not be initialized )[Warning] excess elements in array initializer
[Warning] (near initialization for `pS')
char *pbuffer=buffer; syntax error before "char"
int i=0;
printf("\nYou can enter up to %u message each up to %u characters",NUM_P,BUFFER_LEN-1);
for(i=0;i<NUM_P;i++)
{
pbuffer=buffer;
printf("\nEnter %s message,or press Enter to end\n",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 -maximum %d characters allowed.",BUFFER_LEN);
i--;
continue;
}
*(pbuffer-1)='\0';
pS[i]=(char*)malloc(pbuffer-buffer);
if(pS[i]==NULL)
{
printf("\nOut of menory -ending program.");
rerurn 1;
}
strcpy(pS[i],buffer);
}
printf("\nIn 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;
}
[ 本帖最后由 lanskyxti 于 2014-8-29 14:49 编辑 ]