C Programming - Strings - Discussion
Discussion Forum : Strings - Point Out Correct Statements (Q.No. 2)
2.
Which of the following statements are correct about the below declarations?
char *p = "Sanjay";
char a[] = "Sanjay";
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". |
Discussion:
17 comments Page 1 of 2.
Ravi said:
1 decade ago
I think option 2 is not correct. We cannot modify the string "Sanjay".
Shruti said:
1 decade ago
Why can't the 1st option be correct?
Krishna said:
1 decade ago
In first statement the character pointer p stores the address of the string "Sanjay".
The second statement specifies the space for Sanjay characters be allocated and that the name of location is a[].
The second statement specifies the space for Sanjay characters be allocated and that the name of location is a[].
Girish nischel said:
1 decade ago
yeah! i believe that option 3 is wrong because when we declare
char a[]={"girish"};
the entire array is initialised! hence i believe v cannot change the individual element.if v can really change then wt wl b the output for the following?
char a[]={"giri"};
printf("%c",a[2]);
char a[]={"girish"};
the entire array is initialised! hence i believe v cannot change the individual element.if v can really change then wt wl b the output for the following?
char a[]={"giri"};
printf("%c",a[2]);
Neeraj said:
1 decade ago
We can chnage the individual characters in a string but not the whole string at a time...eg:
#include<stdio.h>
int main()
{
char str[] ="Visual";
*(str+2)='K';
printf("%s",str);
return 0;
}
similarle,to change other characters ,we can put some other value.
#include<stdio.h>
int main()
{
char str[] ="Visual";
*(str+2)='K';
printf("%s",str);
return 0;
}
similarle,to change other characters ,we can put some other value.
Ronil said:
1 decade ago
Is null character added at the end of 2nd statement?I think. Its wrong.
In 1st case null char is added. But for 2nd its wrong.
In 1st case null char is added. But for 2nd its wrong.
Mangusta said:
1 decade ago
I think there is typo in the second statement:
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.
'a' can be considered a constant pointer, pointing to a non-const STRING, not POINTER.
Actually 'a' has type char[7] here, so in reality it is not a pointer, not even constant.
On the other hand, 'p' has type char*, so it is a pointer.
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.
'a' can be considered a constant pointer, pointing to a non-const STRING, not POINTER.
Actually 'a' has type char[7] here, so in reality it is not a pointer, not even constant.
On the other hand, 'p' has type char*, so it is a pointer.
Gaurav Garg said:
1 decade ago
@Ronil.
I agree with your comment. NULL character will not added at the end of second array string. Because array of string by default added NULL character at the end of string.
For eg:
*str1="GAURAV";
*str2[]="GAURAV";
sizeof (str1) = 6;
sizeof (str2) = 7;
I agree with your comment. NULL character will not added at the end of second array string. Because array of string by default added NULL character at the end of string.
For eg:
*str1="GAURAV";
*str2[]="GAURAV";
sizeof (str1) = 6;
sizeof (str2) = 7;
Ahok said:
1 decade ago
What is the meaning of second statement given in the question?
Kaustav said:
1 decade ago
"a" is not a pointer. It is declared as char.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers