C Programming - Strings - Discussion
Discussion Forum : Strings - Yes / No Questions (Q.No. 3)
3.
Is there any difference between the two statements?
char *ch = "IndiaBIX";
char ch[] = "IndiaBIX";
char *ch = "IndiaBIX";
char ch[] = "IndiaBIX";
Answer: Option
Explanation:
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.
The second statement specifies the space for 7 characters be allocated and that the name of location is ch.
Discussion:
5 comments Page 1 of 1.
Mohamed said:
5 years ago
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.
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.
Akshay Immanuel said:
9 years ago
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)?
Like ch[3] (pointer declaration of ch) == ch[3] (array declaration of ch)?
Arda said:
6 years ago
So for both cases what is ch[0], ch[1], ch[2], ch and *ch?
James said:
1 decade ago
It would be 9 characters - 8 plus one for NULL.
Ramandeep said:
8 years ago
Yes, you are Right @James.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers