Exercise "Example is better than precept."
- (Proverb)
1.
Will the program compile successfully?
#include<stdio.h>
int main()
{
char a[] = "India";
char *p = "BIX";
a = "BIX";
p = "India";
printf("%s %s\n", a, p);
return 0;
}
Answer: Option D
Explanation:
Because we can assign a new string to a pointer but not to an array a .
2.
For the following statements will arr[3] and ptr[3] fetch the same character?
char arr[] = "IndiaBIX"; char *ptr = "IndiaBIX";
Answer: Option E
Explanation:
Yes, both the statements prints the same character 'i'.
3.
Is there any difference between the two statements?
char *ch = "IndiaBIX"; char ch[] = "IndiaBIX";
Answer: Option E
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 .