![]() |
VOOZH | about |
An array in C or C++ is a collection of items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They are used to store similar types of elements as in the data type must be the same for all elements. They can be used to store the collection of primitive data types such as int, float, double, char, etc of any particular type. To add to it, an array in C or C++ can store derived data types such as the structures, pointers, etc. There are two types of arrays:
data_type variable_name[size] data_type is the type of array, like int, float, char, etc. variable_name is the name of the array. size is the length of the array which is fixed.Note: The location of the array elements depends upon the data type we use. Below is the illustration of the array: 👁 Image
1 2 3 4
data_type variable_name[N][M] data_type is the type of array, like int, float, char, etc. variable_name is the name of the array. N is the number of rows. M is the number of columns.Below is the program to illustrate the traversal of the 2D array:
1 2 3 4
C++ string class internally uses character array to store character but all memory management, allocation, and null termination are handled by string class itself that is why it is easy to use. For example it is declared as:
char str[] = "GeeksforGeeks"Below is the program to illustrate the traversal in the string:
G e e k f o r G e e k s
Length of string GeekforGeeks is 12 String GeekforGeeks and String HelloGeek are not equal. String str1 before: GeekforGeeks String str1 after: HelloGeek