大侠,好像不行哦!
----------------解决方案--------------------------------------------------------
C好像没有办法,只定义得足够大(可以应付所有情况),如果是C++的话可以用vector解决这个问题
----------------解决方案--------------------------------------------------------
以下是引用laigaoat2005在2007-4-23 21:39:12的发言:
刚才的有问题,现在好了:
#include
main()
{
int n=0;
int i;
int num[n];
printf("输入数组大小:\n");
scanf("%d",&n);
printf("你的数组大小是:%d\n请输入所有的数\n",n);
for(i=0;i
{
scanf("%d",&num[i]);
}
printf("你输入的数为:\a\n");
for(i=0;i
{
printf("%d\n",num[i]);
}
getchar();
}
c里面不能把数组大小定义为变量;int num[n];
----------------解决方案--------------------------------------------------------
13楼在C语言中你的这个程序中数组的下标是不能用变量来代替的
----------------解决方案--------------------------------------------------------
13楼的别费劲了,告诉你,在c中,数组的大小一定是在声明是就决定好的了,所以你那个程序永远也不可能对.
数组大小一定是个常量,绝对不能是个变量.
要实现楼主要的功能 就用 动态分配吧,单链表可以实现.
/*说明 Elemtype 为任意的数据类型 如:int float char等 这里默认为int*/
/*单链表的节点类型node定义*/
typedef struct linknode
{
Elemtype data;
struct linknode *next;
}node;
/*建立一个单链表,输入一系列整数,以0做结束标志*/
void creat()
{
node *head,*p,*s;
int x, cycle=1; /*cycle是循环变量*/
head=(node*)malloc(sizeof(node)); /*建立头节点*/
p=head;
while(cycle)
{
scanf("%d",&x);
if(x!=0)
{
s=(node*)malloc(sizeof(node));/*建立后续节点*/
s->data=x;
p-next=s;
p=s;
}
else cycle=0;
}
p->next=NULL;
p=head;
head=heda>-next; /*删除头节点*/
free(p);
}
----------------解决方案--------------------------------------------------------
C99标准中的变长数组可以实现你的要求。
----------------解决方案--------------------------------------------------------