首页 > 编程知识 正文

c语言中如何定义动态数组,数组元素初始化

时间:2023-05-04 13:19:29 阅读:163435 作者:4209

一.定义

数组的维数必须用1以上的恒等式定义

整形并初始化文字常量、枚举常量或常量表达式的整形const对象;

二.初始化

1、显示初始化数组元素

*在函数外部定义的所有内置数组的元素都初始化为0。

*函数中定义的内置数组。 该元素未初始化;

*无论数组在哪里定义,如果元素是类类型,则会自动调用该类的默认构造函数进行初始化。 如果类没有缺省构造函数,则必须初始化数组元素的显示

2、特殊字符数组

3、不允许直接复制和赋值数组

//shuzu.CPP : definestheentrypointfortheconsoleapplication.//# include ' STD afx.h ' # include iostream # include stre i=i 1; return i; } class test class { public : protected : private : inta; char* p; (; int atest[10]; testClass atc[10]; int_tmain(intargc,_TCHAR* argv[] ) { const unsigned buf_size=512,max_files=20; int staff_size=27; const unsigned sz=get_size (; //sz是const对象,但必须是char input_buffer[buf_size]; //buf_size是const常量string fileTable[max_files 1]; //ok max_files是const常量,max_files 1是编译时21//double salaries[staff_size]; //errornonconstvariable//int test _ scores [ get _ size () ]; //errornotconstexpression//intvals [ SZ ]; //errorsizenotknownuntilruntime//初始化数组元素int btest[10]; 测试类BTC [ 10 ]; //特殊字符数组char ca1[]={'c ',',' };//三维char ca2[]={'c ',',',',','0'}; //4维char ca3[]='c ';//四维nullterminatoraddedautomatically//不允许直接复制和赋值数组的intia [ ]={ 0,1,2 }; //intia2[](ia ); //error int ia3[]; //ia3=ia; //error return 0; }

转载于:https://www.cn blogs.com/yin apan/p/3889217.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。