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 2 of 2.
Suresh Dave said:
1 decade ago
Depending on compiler first option(2) may or may not be correct.
In turbo C it stores string pointed by pointer during initialization in data section whereas in gcc its in code section which is read only memory!
In turbo C it stores string pointed by pointer during initialization in data section whereas in gcc its in code section which is read only memory!
Jahnavi said:
1 decade ago
I'm not understsanding second option please explain.
Anusha said:
1 decade ago
I'm not understanding any answer please explain clearly.
RBJII said:
1 decade ago
@GAURAV.
In *str also the NULL char is added.
For e.g: We undergo to find the length of string
char *str="BALAJI";
int length;
while(*str!='\0')
{
length++;
str++;
}
return(length);
In the above program, it clearly shows that only after detecting the '\0' on *str, the while() condition gets terminates, Hence the NULL char will be default. Don't confuse with Sizeof(). Statement is in both cases the '\0' will be ADDED at the end of the string "Sanjay" is TRUE.
In *str also the NULL char is added.
For e.g: We undergo to find the length of string
char *str="BALAJI";
int length;
while(*str!='\0')
{
length++;
str++;
}
return(length);
In the above program, it clearly shows that only after detecting the '\0' on *str, the while() condition gets terminates, Hence the NULL char will be default. Don't confuse with Sizeof(). Statement is in both cases the '\0' will be ADDED at the end of the string "Sanjay" is TRUE.
Siyah said:
7 years ago
@All.
The program is to swap the first alphabet of the two strings. Can anyone tell the mistake?
#include<stdio.h>
int main(int argc, char* argv[])
{
char *c;
char *c1;
char *temp;
c=argv[1];
c1=argv[2];
temp[0]=c[0];
c[0]=c1[0];
c1[0]=temp[0];
printf("%s",c);
printf("%s",c1);
return 0;
}
The program is to swap the first alphabet of the two strings. Can anyone tell the mistake?
#include<stdio.h>
int main(int argc, char* argv[])
{
char *c;
char *c1;
char *temp;
c=argv[1];
c1=argv[2];
temp[0]=c[0];
c[0]=c1[0];
c1[0]=temp[0];
printf("%s",c);
printf("%s",c1);
return 0;
}
Vinaykumar said:
7 years ago
I think option C is correct. Because in char *p="Sanjay". Pointer p is in stack but that pointer pointing string 'Sanjay" is store in read-only text section. So it is a const string. That too char a[]="Sanjay" in that statement a is not a const pointer. So I think option C is correct.
Harish said:
6 years ago
Option 2 is wrong. Because p is a no constant pointer but it is pointing to the constant string (that string will be stored in the data segment in read only memory location).
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers