top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference between array of character and string?

+1 vote
624 views
What is the difference between array of character and string?
posted Mar 7, 2018 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

In C language, string is collection of characters followed by NULL character, while array of character is collection of characters only without NULL character at last. For example:
char array[ ] = { 'a', 'b', 'c' }; /* Example of array of characters */
char string = "abc"; /* Example of string */

answer Mar 8, 2018 by Harshita
There is a typo in char string = "abc"; I think you intended to have
char *string = "abc"
the diff between a char *string and char arr[] is that the base of (any) array is a const pointer whereas, for the pointer, its not (unless you make the pointer const)

Even though, both string and array variables point to the base of the array.

ex:
char *string = "abc";
string ++;
printf("%s", string);
//would print bc because, string was pointing to base of the array abc which had a.
char aray[] = {'a','b','c'};
array ++; // this is an invalid operation and compiler would throw an error as attempting to modify an lvalue because base of this (and any array) is a const pointer.
Thanks to correct me. I missed  * before string variable.
...