1,306 views
4 votes
4 votes

Which of the following statements are correct about the below declarations?
char *p = "Sanjay";
char a[] = "Sanjay";

1: There is no difference in the declarations and both serve the same purpose.
2: p is a non-const pointer pointing to a non-const string, whereas a is a const pointer pointing to a non-const pointer.
3: The pointer p can be modified to point to another string, whereas the individual characters within array a can be changed.
4: In both cases the '\0' will be added at the end of the string "Sanjay".
[A]. 1, 2
[B]. 2,3,4
[C]. 3, 4
[D]. 2, 3

2 Answers

0 votes
0 votes

Statement is False:

p is a pointer to a string literal, whereas a is a pointer to an array of  7 characters.

Statement is False:

p is a non-const pointer pointing to a const string, whereas a is a const pointer pointing to a non-const string

Statement is True:

Statement is True:

Answer is C

0 votes
0 votes
char *p="Sanjay";

/* p is a pointer to an array of characters (string, ending with '\0')

   p is a non-const pointer (as it can be modified to point to a different array of characters,string(aka!))

   "Sanjay" is a constant string (as it cannot be modified to a different array of characters)

*/

char a[]="Sanjay";

/* a is a pointer to an array of characters (string, ending with '\0')

   a is a const pointer (as it cannot be modified to point to a different array of characters,string(aka!))

   "Sanjay" is a non-constant string (as it can be modified to a different array of characters)

*/

Considering these facts, (1) & (2) arguments are false.

So, correct answer is (C)

Related questions

0 votes
0 votes
1 answer
1
SSR17 asked Feb 29
205 views
#include <stdio.h int main() { int i = -1; int x = (unsigned char)i; printf("%d", x); return 0; }output is 255 , but please explain how
2 votes
2 votes
1 answer
2
rupamsardar asked Aug 30, 2023
468 views
#include <stdio.h int f(int x) { if(x%2==0) { return f(f(x-1)); } else return (x++); } int main() { printf("%d",f(12)); ret...
5 votes
5 votes
2 answers
3
saurabh0709 asked Aug 1, 2023
1,136 views
What will be the output of the following code? _______ #include <stdio.h int main(){ char val=250; int ans; ans= val+ !val + ~val + ++val; printf("%d", ans); return 0; }
2 votes
2 votes
1 answer
4