Difference Between Array And String In C

Hi, In this post we are going to discuss one of the most favorite questions of C which is “Difference Between Array and String in C“.

However, If you want to read “How to concatenate two strings” you can head to this awesome article.

Now before starting the discussion of Array and String, I think we should also discuss some basics of Array and String.

So, without wasting your time let’s start

What is An Array

According to Google, “Array is a collection also a Data Structure which is used to store data of the same type”.

Array has a fixed Size. Simple, when you want to store multiple integers you may do like this

int a;
int b;
int c;
int d;
int e;
int f;
int g;

But also there is a better way of doing this which is array, you can do same thing via array like this

int arr[7];

// For inserting value

arr[0] = 1;
arr[1] = 2;

// You can also use loop
for(int i = 0;i<7;i++)
{
arr[i] = i;
}

You can do this to store multiple integer, double, char etc.

What is an String

String is nothing but array of character. Yes, you can view String as an different type of array.

String is same as array but the main difference is that it is an array of characters and always terminated with a null character.

Points to remember for String are

  1. String is a sequence of characters
  2. strings are immutable
  3. character can be accessed using index
  4. string can hold only character

Let’s not confuse you and come to the example

Suppose, you want to store your name in array, How would you do that

	char arr[5];
	arr[0]='a';
	arr[1]='b';
	arr[2]='c';
	arr[3] ='d';
	arr[4]='e';
	printf("%s",arr);

When you run the above code in C language you will see the output abcde

Now, you can say in above code we create an array of type char.

Now, we will create a string and we can do this like –

	char arr[]="abcde";
	printf("%s",arr);

You can see we do the same thing in two different ways.

Now, we know the basics of array and the string. It’s time to discuss the Difference between array and string in C

Difference Between Array And String In C

As you guess, Array and String are very same but also there are differences which we are going to discuss

Difference In Dimension –

The very first difference is the difference in dimension. You can create Single Dimension or 2 Dimensional Array but in case of string you can not create 2 Dimension.

Example

Creating 2D Array in C is simple and you can do like this

char arr[3][3];

When you run above code no error will appear. But when you try to do the same using this like

char arr[][] = { "Blue", "Red", "Orange",
						"Yellow" };

Then you will some error which are listed below

declaration of ‘arr’ as multidimensional array must have bounds for all dimensions except the first
  char arr[][] = { "Blue", "Red", "Orange",""Yellow"};

Hence you need to provide the second array size and this code works fine if you do something like this

	char arr[][10] = { "Blue", "Red", "Orange",
						"Yellow" };

Difference In Length

Whenever you declared array like this

char arr[10];

You fixed the size of array. In this case you fixed it to the size 10. you are not able to change the size.

Same rule apply to string but here if you use pointer you can increase the size of string if you want.

Note – Length of Array can be change using malloc, calloc function

Short the Length Of String Array

Here, We are going to show you a magic. We are going to short the length of String

See the below code and predict what is the output

#include <stdio.h>
#include <string.h>

int main() {
	//code
	
char a[] = "Geekstocode";

printf("%s \n",a);

strcpy(a, "geeks");

printf("%s",a);
	return 0;
}

Output of the above code is

Geekstocode
geeks

See, how we can change the length of String from 11 to 5. Now you can google how we changes the string length

There is a hint – If you go deeply you will find we did not change the length here

Difference In Data Type

This the basic difference which every one know, Array can store sets of integer, Double, Float, Char etc. But String only store the char.

There is a Question for you. Can you predict the output of the following

char arr[]="12344";
printf("%s",arr);

If you are thinking that string store only integers and the above code produce error, then you are wrong.

Here, arr is treated as a string and 12344 as character not integers

Conclusion

Hope you find this post helpful. If you have any doubt or you want to rectify this post please comment here. Thanks for be here and Happy Coding.

Leave a Comment

Your email address will not be published. Required fields are marked *