首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

瞧瞧这个10行的c程序吧解决方法

2012-02-25 
瞧瞧这个10行的c程序吧#include stdio.h main(){intnprintf( Entern: )scanf( %d ,&n)inta[n]{0

瞧瞧这个10行的c程序吧
#include "stdio.h "
main()
{     int   n;
      printf( "Enter   n: ");
      scanf( "%d ",&n);
      int   a[n]={0};
      for(i=0;i <n;i++)
      printf( "%d, ",a[i]);
      getch();
}
很显然这个程序是有错误的,在写这个东西时我是这样想的,要定义个数组,要让用户来定义它的大小,如果让是程序员来定义可能会不够大不够用,或者有的时候定义大了又很浪费内存空间,但是这个想法不能实现,很可惜。谁能给我解释下吗?

[解决办法]
如果是C可以用malloc
C++ 可以用new
也可以用STL提供的变长容器~
[解决办法]
int a[n]={0};

n必须确切知道,你可以在前面让用户定义
[解决办法]
#include "stdio.h "
#include "stdlib.h "
#include "memory.h "

// ============================================================================
// ==============================================================================
int main()
{
//~~~~~
int i, n;
//~~~~~

printf( "Enter n: ");
scanf( "%d ", &n);

//~~~~~~~~~~~~~~~~
int *a = new int[n];
//~~~~~~~~~~~~~~~~

memset(a, 0, n * sizeof(int));

for (i = 0; i < n; i++) {
printf( "%d, ", a[i]);
}

delete[] a;

system( "pause ");
return 0;
}

[解决办法]
int a[n]={0};

数组下表一定要是常数不能是变量
[解决办法]
Piao_Polar(飘) ( ) 的程序基本可以实现了

貌似还有个办法是在结构体中最后定义一个0长的数组指针.
[解决办法]

int * a = (int*)malloc(n*sizeof(int));

for (int i=0; i <n; i++) {
a[i] = .......
}

......

free(a);
[解决办法]
在gcc里面是可以由用户来确定数组长度的
不过还是用vector方便呀
[解决办法]
C99支持变长数组的,这样可以编译通过
[解决办法]
数组需要在编译期知道长度,而n的值是在运行期才知道的。可通过vector定义动态数组
[解决办法]
Specifying an Array Size
So far, the examples have used integer constants when declaring arrays:


#define SIZE 4

int main(void)

{

int arr[SIZE]; // symbolic integer constant

double lots[144]; // literal integer constant

...


What else is allowed? Until the C99 standard, the answer has been that you have to use a constant integer expression between the brackets when declaring an array. A constant integer expression is one formed from integer constants. For this purpose, a sizeof expression is considered an integer constant, but (unlike the case in C++) a const value isn 't. Also, the value of the expression must be greater than 0:


int n = 5;

int m = 8;

float a1[5]; // yes

float a2[5*2 + 1]; // yes

float a3[sizeof(int) + 1]; // yes

float a4[-4]; // no, size must be > 0

float a5[0]; // no, size must be > 0

float a6[2.5]; // no, size must be an integer



float a7[(int)2.5]; // yes, typecast float to int constant

float a8[n]; // not allowed before C99

float a9[m]; // not allowed before C99


As the comments indicate, C compilers following the C90 standard would not allow the last two declarations. C99, however, does allow them, but they create a new breed of array, something called a variable-length array, or VLA for short.

C99 introduced variable-length arrays primarily to allow C to become a better language for numerical computing. For example, VLAs make it easier to convert existing libraries of FORTRAN numerical calculation routines to C. VLAs have some restrictions; for example, you can 't initialize a VLA in its declaration. This chapter will return to VLAs later, after you 've learned enough to understand more about the limitations of the classic C array.

[解决办法]
C99可以,89不行,C99的变长数组用 _alloca 实现, 稀饭的话你也可以用三 ...
[解决办法]
上面的哪个“飘”,你的程序错了把
我怎么编译不出来
哨加改变就可以了。
[解决办法]
给个C++的版本给你(代码没编译过,语法可能不完全对啊.^_^。)

#include <vector>
#include <iostream>
using namespace std;


int main(int argc, _TCHAR* argv[])
{
cout < < "Please input a number> ";
int n;
cin > > n;
vector <int> a(n);
std::copy(a.begin(), a.end(), ostream_iterator <int> (cout, "\n "));
return 0;
}


[解决办法]
vector
[解决办法]
在函数过程里直接定义的数组(例如:int a[10]; )是在栈里分配内存的,这个工作是由编译器完成的。也就是说,在编译程序的时候,一定要知道这个数组的确切大小。
[解决办法]
C99支持变长数组的,这样可以编译通过

[解决办法]
我不要分,我只想说:楼主您学过 C 语言没?
[解决办法]
C 用malloc
C++ 用new

[解决办法]
呵呵。C里面只有动态分配空间才行哦。

[解决办法]
倒是比较感兴趣C99中VLA是怎么实现的

热点排行
Bad Request.