C或C++中的数组是存储在连续内存位置的项目的集合,可以使用数组的索引随机访问元素。它们用于存储相似类型的元素,因为所有元素的数据类型必须相同。它们可用于存储原始数据类型的集合,例如任何特定类型的int,float,double,char等。作为补充,使用C或C++的数组可以存储派生的数据类型,例如结构,指针等。下面给出的是数组的如诗如画的表示形式。
C++中有两种类型的数组/字符串声明,如下所示
- 静态声明
- 动态声明
方法1:静态声明
方法:静态声明可以通过三种方式完成
- 通过指定大小来声明数组
- 通过初始化元素进行数组声明
- 通过指定大小和初始化元素进行数组声明
通过指定大小来声明数组
句法:
int arr1[10];
// Array declaration by specifying size
int n = 10;
int arr2[n];
Note: With recent C/C++ versions, we can also declare an array of user-specified size
通过初始化元素进行数组声明
int arr[] = {10, 20, 30, 40};
// Array declaration by initializing elements
Compiler creates an array of size 4. This is same as “int arr[4] = {10, 20, 30, 40}”
通过指定大小和初始化元素进行数组声明
句法:
int arr[6] = {10, 20, 30, 40} ;
// Array declaration by specifying size
// and initializing elements
Compiler creates an array of size 6, initializes first 4 elements as specified by user and rest two elements as above is same as follows:
Note: In a static declaration, the memory will get allocated in the stack memory.
实现:下面是演示静态声明的C程序
范例1:
C
int arr[] = {10, 20, 30, 40, 0, 0}"
C
// C Program to Implement Static Declaration
// Importing standard input output operations file
#include
// Main driver method
int main()
{
// Static declaration of array so it
// will get memory in Stack memory
int first_array[2];
// Assign value 10
first_array[0] = 10;
// Assign value 20
first_array[1] = 20;
// Printing value
printf("%d %d", first_array[0],
first_array[1]);
return 0;
}
C
// C program to implement Dynamic Memory Allocation
// using Malloc
// Importing standard input output files
#include
// Include this library for malloc
#include
// Main driver method
int main()
{
// Memoru will get allocated in Heap and
// RHS is implcitly typecasted to integer as
// LHS is having integer as a return type
int *first_array = (int*)malloc(sizeof(int)*2);
// Creating and initializing array
// Custom integer values on indicis as specified
first_array[0] = 10;
first_array[1] = 20;
// Printing elements of both the arrays
printf("%d %d", first_array[0],
first_array[1]);
return 0;
}
// C Program to implement Alternative to Malloc
// Importing standard input output file
#include
// Main driver method
int main()
{
// Alternative of Malloc '*' operator refers
// to address of array memory block
int *first_array = (int[2]){};
// Creating and initializing array together
// Custom input element at array indicies
first_array[0] = 10;
first_array[1] = 20;
// Printing the array indicies as passed in argument
printf("%d %d", first_array[0],
first_array[1]);
return 0;
}
动态声明: Malloc用于动态分配数组。在这种情况下,内存将在heap中分配。程序完成后,分配的内存将释放。
范例2:
C
10 20
// C program to implement Dynamic Memory Allocation
// using Malloc
// Importing standard input output files
#include
// Include this library for malloc
#include
// Main driver method
int main()
{
// Memoru will get allocated in Heap and
// RHS is implcitly typecasted to integer as
// LHS is having integer as a return type
int *first_array = (int*)malloc(sizeof(int)*2);
// Creating and initializing array
// Custom integer values on indicis as specified
first_array[0] = 10;
first_array[1] = 20;
// Printing elements of both the arrays
printf("%d %d", first_array[0],
first_array[1]);
return 0;
}
现在达到了一个古怪的目标,即寻找一种分配内存而不是动态声明的替代方法
范例3:
C
10 20
// C Program to implement Alternative to Malloc
// Importing standard input output file
#include
// Main driver method
int main()
{
// Alternative of Malloc '*' operator refers
// to address of array memory block
int *first_array = (int[2]){};
// Creating and initializing array together
// Custom input element at array indicies
first_array[0] = 10;
first_array[1] = 20;
// Printing the array indicies as passed in argument
printf("%d %d", first_array[0],
first_array[1]);
return 0;
}