top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference between strings and character array in C?

+5 votes
556 views
What is the difference between strings and character array in C?
posted Nov 17, 2013 by anonymous

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

2 Answers

0 votes

A major difference is: string will have static storage duration, whereas as a character array will not, unless it is explicitly specified by using the static keyword.

Actually, a string is a character array with following properties:

  • The multibyte character sequence, to which we generally call string, is used to initialize an array of static storage duration. The size of this array is just sufficient to contain these characters plus the terminating NULL character.

  • It not specified what happens if this array, i.e., string, is modified.

  • Two strings of same value may share same memory area. For example, in the following declarations:

char *s1 = “Calvin and Hobbes”;
char *s2 = “Calvin and Hobbes”;

the strings pointed by s1 and s2 may reside in the same memory location. But, it is not true for the following:

char ca1[] = “Calvin and Hobbes”;
char ca2[] = “Calvin and Hobbes”;

  • Array name is constant where as string is a variable i.e. s1++ is a valid statement whereas ca1++ is a invalid statement.
answer Nov 17, 2013 by Salil Agrawal
0 votes

IN C there is no big diffrence Between string and character array

but string must be NULL terminated...... if u ll forget to insert the '' at the END it will automatically insert the NULL chracter .take a example

int i;
char arr[5]={'a','s','d','z','x'}; \no error because not a string..it is collection of chracter
char arr1[5]="asdzx"; //error overflow because last chracter must be
char arr2[6]="asdzx";//will work
for (i=0;i<5;printf("n%d----%cn",i,arr[i]),i++);

printf("nstringn ");
printf("nstring-----is ----%sn",arr1);

answer Dec 10, 2015 by Rajan Paswan
Similar Questions
+3 votes
#include<stdio.h>
#include<string.h>

int main()
{
    char ptr[]= {'a','b','c','0','e'};
    char str[]= "abc0e";
    printf("\nptr = %s\t len = %d\n",ptr, strlen(ptr));
    printf("\nstr = %s\t len = %d\n",str, strlen(str));
    return 0;
}

Output : ptr = abc0e len = 6
str = abc0e len = 5

Why the length for ptr is 6 ? Can someone please explain it ?

+1 vote

int arr[ ] = { 1, 2 };
p = arr; /* p is pointing to arr */

How pointer p will behave ?

...