
C语言中定义动态数组的方法 5
3个回答
展开全部
long *buffer; // 定义一个动态数组
...malloc.... // 动态分配 1000 单元
...realloc.... // 再添加 分配 1000 单元
free( buffer ); // 取消 单元
=============================================
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
void main( void )
{
long *buffer;
size_t size;
if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )
exit( 1 );
size = _msize( buffer );
printf( "Size of block after malloc of 1000 longs: %u\n", size );
/* Reallocate and show new size: */
if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) ))
== NULL )
exit( 1 );
size = _msize( buffer );
printf( "Size of block after realloc of 1000 more longs: %u\n",
size );
free( buffer );
exit( 0 );
}
...malloc.... // 动态分配 1000 单元
...realloc.... // 再添加 分配 1000 单元
free( buffer ); // 取消 单元
=============================================
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
void main( void )
{
long *buffer;
size_t size;
if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )
exit( 1 );
size = _msize( buffer );
printf( "Size of block after malloc of 1000 longs: %u\n", size );
/* Reallocate and show new size: */
if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) ))
== NULL )
exit( 1 );
size = _msize( buffer );
printf( "Size of block after realloc of 1000 more longs: %u\n",
size );
free( buffer );
exit( 0 );
}
展开全部
int arg[] = new int[45];
可以用变量代替 45;
可以用变量代替 45;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不知道我是不是理解你的意思了哈
new操作符
如(括号里的不要输入进程序)
int(任何类型) *list(指针名)=new int(与前面第一个的类型一致)[m(变量与表达式,常量)];
定义完就可以使用了,与普通的数组一样。另外在定义二位数组时,第二维必须为常量,同样,多维数组只能用变量与表达式,常量来定义第一为,否则不安全
new操作符
如(括号里的不要输入进程序)
int(任何类型) *list(指针名)=new int(与前面第一个的类型一致)[m(变量与表达式,常量)];
定义完就可以使用了,与普通的数组一样。另外在定义二位数组时,第二维必须为常量,同样,多维数组只能用变量与表达式,常量来定义第一为,否则不安全
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询