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:
- One Dimensional Array
- Multi Dimensional Array
One Dimensional Array: A one dimensional array is a collection of same data types. 1-D array is declared as:
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 program to illustrate the traversal of the array:
1 2 3 4
MultiDimensional Array: A multidimensional array is also known as array of arrays. Generally, we use a two-dimensional array. It is also known as the matrix. We use two indices to traverse the rows and columns of the 2D array. It is declared as:
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
The string data_type in C++ provides various functionality of string manipulation. They are:
- strcpy(): It is used to copy characters from one string to another string.
- strcat(): It is used to add the two given strings.
- strlen(): It is used to find the length of the given string.
- strcmp(): It is used to compare the two given string.
Below is the program to illustrate the above functions:
Length of string GeekforGeeks is 12 String GeekforGeeks and String HelloGeek are not equal. String str1 before: GeekforGeeks String str1 after: HelloGeek
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count of same length Strings that exists lexicographically in between two given Strings
- Search in an array of strings where non-empty strings are sorted
- Meta Strings (Check if two strings can become same after a swap in one string)
- Pairs of complete strings in two sets of strings
- Number of common base strings for two strings
- Count of strings that become equal to one of the two strings after one removal
- Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum
- Count of possible arrays from prefix-sum and suffix-sum arrays
- Merge k sorted arrays | Set 2 (Different Sized Arrays)
- Distinct strings such that they contains given strings as sub-sequences
- Find sub-arrays from given two arrays such that they have equal sum
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Maximum OR sum of sub-arrays of two different arrays
- Add n binary strings
- <strings> library in C++ STL
- XOR of two Binary Strings
- Strings in C++ and How to Create them?
- Concatenation of two strings in PHP
- Comparing two strings in C++
- Shortest possible combination of two strings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.