What is the Difference Between Array and String

The main difference between Array and String is that an Array is a data structure that stores a set of elements of the same data type while a String is a set of characters.

Programming languages such as C supports arrays and strings. An array is a fixed size data structure that stores data elements that belong to the same type. It is a collection of variables with the same name that can access the array index. Moreover, it represents a list of elements. On the other hand, string is similar to an array but it only consists of characters. In other words, it is a sequence of characters that denote a single data element. 

Key Areas Covered

1. What is an Array
      – Definition, Functionality
2. What is a String
     – Definition, Functionality
3. What is the Difference Between Array and String
     – Comparison of Key Differences

Key Terms

Array, String

Difference Between Array and String - Comparison Summary

What is an Array

An array helps to store elements of the same data type. When declaring an array, the programmer has to give the number of elements the array can store. Therefore, an array is a data structure with a fixed length. Refer the below program.

Difference Between Array and String

Figure 1: Program with an Array

In the above program, numbers is an array. It can store 10 integers. Moreover, the value is an integer variable. Its initial value is 0. Inside the “for loop”, the calculated value is assigned to the array. The next for loop iterates through the numbers and print them on the console. We can easily access an array element, like in line 14.

The starting index of an array is 0. Therefore, in an array of 10 elements, the index of the last element is 9. Moreover, an array stores elements in contiguous memory allocation. The first index has the lowest address whereas the last index has the highest address. As an array has a fixed size, it is not possible to assign the number of elements higher than that the declared amount. In other words, we cannot store 15 elements in an array with an array size of 10.

Another common array type is a multi-dimensional array. It stores elements according to a format consisting of rows and columns similar to a table.

What is a String

String is a set of characters. It ends with a null character which is ‘\0’. A programmer can store a String similar to an array. The following statement is the declaration and initialization of a string.

char message[6] = {‘C’, ‘o’, ‘l’, ‘o’, ‘u’, ‘r’, ‘\0’};

We can also write the above statement as follows.

char message[]= “Colour”; 

As it is necessary to store the null character, the string size should be one more than the number of characters in the string. In C language, we declare a string using “char” data type. It is not necessary for the programmer to place a null character at the end of the string. The compiler places a null character automatically at the end of the string at the time of initializing the array.

Key Difference - Array vs String

Figure 2: Program with an array

The “string1” is a string. The printf statement displays it on the console. The strlen function helps to find the total number of characters in the string. The newString1 can store 7 characters. The strcpy function helps to copy the string1 to newString1. So, newString1 also have the string “Colour”.  Furthermore, newString2 has the string “Purple”. The strcat helps to concatenate string1 and newString2. It combines the newString2 at the end of string1.

Difference Between Array and String

Definition

An array is a data structure consisting of a collection of elements each identified by the array index while a string is a one-dimensional array of characters terminated by a null character. Thus, this is the main difference between Array and String.

Data Type

Another difference between Array and String is that an array can store a set of integers, doubles, floats, etc. while a string can only store characters.

Size

Also, size is  one other difference between Array and String. An array has a fixed size. However, though a string also has a fixed size, it can be changed when using a char pointer.

Type

Furthermore, an array can be one-dimensional or two dimensional, but a string is always two dimensional.

Conclusion

The main difference between Array and String is that Array is a data structure that stores a set of elements of the same data type while String is a set of characters. In brief, String is an array but it only stores characters. On the other hand, an array can store a set of integers, set of double, set of characters, etc. The data type of an array is not limited to characters.

Reference:

1.“C Array – Javatpoint.” Www.javatpoint.com, Available here.
2.“C Strings – Javatpoint.” Www.javatpoint.com, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply