What is the difference between Char a[ ]=”string” and char
*a=”String”
Answers were Sorted based on User's Feedback
Answer / prasant nayak
differences are as follows
1.
Char a[]="string"; //invalid, coz--its not 'Char' but
its 'char' , i.e its syntaticaly incorrect
char *a="string";//it correct
2.
char a[]="string";
above 'a' is an array of characters , where we can change
the string, its not a constant.
i.e we can do a[3] = 'Z';
char *a = "string";
above 'a' is a string constant where we can't change the
string i.e we can't do a[3] = 'Z';
Is This Answer Correct ? | 42 Yes | 6 No |
Answer / sushant mahajan
Ohk... dudes I think that 'C' in Char a[] is a typo. The only difference I can see is this:
char *a="string";
printf("%d", sizeof(a));
/*will print 2 or 4, whatever is the memory assigned to a pointer*/
char a[]="string";
printf("%d", sizeof(a));
/*will print 7 - 1 byte for each character+1 byte for the '\0' */
So basically its a memory thing :D
Is This Answer Correct ? | 4 Yes | 0 No |
Answer / kanakesh
using char*a we can't assign new individual character like
a[2]='k';
Is This Answer Correct ? | 10 Yes | 9 No |
Answer / vikram
char a[]="string" refers that string has constant length and it will reserve fixed memory while in case of char *a="string" the string has variable length
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / thinker
Losers.! char* is not a constant pointer where as char[] is a constant pointer
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / rajesh kumar
char *p="string" has RO permission while its counterpart char a[]="string" has RW permission.
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / anshul
I guess the main difference for which interviewer was looking for it that name of the array is constant pointer and character pointer is not. So, if
char a1[]="string";
char *a2="string";
a2=a1;//Compiles and works perfectly
a1=a2;//Error caught at compile time
This is a famous example and is available in many C books.
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / ratish
c/c++ is case sensitive language. so Char is invalid data type. hence char *a="String" is correct answer.
Is This Answer Correct ? | 11 Yes | 14 No |
Answer / vincy
the syntax for a character variable is wrong if we use a
pointer variable as given above that is char*a="string". so
i think the first one is right.we know that the pointer
variable points to the value in the particular address, then
it should not the "string" but "&string".
Is This Answer Correct ? | 0 Yes | 4 No |
Answer / niranjan kumar niraj
char a[]="string" returns characters and char *a[]="String"
returns address of character
Is This Answer Correct ? | 1 Yes | 7 No |
Is multimap sorted c++?
What is a parameterized type?
Reverse the Linked List. Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL
What is pointer in c++ with example?
Why do we use string in c++?
What operator is used to access a struct through a pointer a) >> b) -> c) *
Function can be overloaded based on the parameter which is a value or a reference. Explain if the statement is true.
What is a tuple c++?
What is the difference between Pointer and a Reference? When you would use them?
What are the data types in c++?
What is nested class in c++?
What are advantages of c++?