C Programming - Strings - Discussion


Is there any difference between the two statements?
char *ch = "IndiaBIX";
char ch[] = "IndiaBIX";

[A]. Yes
[B]. No

Answer: Option A


In first statement the character pointer ch stores the address of the string "IndiaBIX".
The second statement specifies the space for 7 characters be allocated and that the name of location is ch.

James said: (Aug 14, 2015)  
It would be 9 characters - 8 plus one for NULL.

Akshay Immanuel said: (Jul 2, 2016)  
By dereferencing the pointer and the array, won't they be the same?

Like ch[3] (pointer declaration of ch) == ch[3] (array declaration of ch)?

Ramandeep said: (May 25, 2017)  
Yes, you are Right @James.

Arda said: (Jun 8, 2019)  
So for both cases what is ch[0], ch[1], ch[2], ch and *ch?

Mohamed said: (Jun 13, 2020)  
Yes, there is a difference, first statement apointer (ch) is a non-constant pointer to non-constant string because we can assign pointer to another string, sizeof(ch) depends on the compiler and operating system.

But in second statement array of string, the name of an array is a constant pointer, we cannot assign the string in array to another string,sizeof(ch)in this statement is 8.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.