Bloomberg LP Interview Question
Software Engineer / DevelopersI guess, the difference between (1) Char a[]=”abc” and (2) char *a=”abc” is that,
(1) is a constant with size of 3 chars.
(2) is variable with variable size.(3 chars in this case)
to answer second part of the question i will copy/paste from
http://www.lysator.liu.se/c/c-faq/c-2.html
<paste>
The array declaration "char a[6];" requests that space for six characters be set aside, to be known by the name "a." That is, there is a location named "a" at which six characters can sit. The pointer declaration "char *p;" on the other hand, requests a place which holds a pointer. The pointer is to be known by the name "p," and can point to any char (or contiguous array of chars) anywhere.
</paste>
"a" in "a[]" is a constant pointer to a char , it cant be dereferenced . where as "a" in "*a" is a normal pointer to char it can be dereferenced.
you can change content of a[] where all the char from literal are4 copied to a char array . but you can not change "abc" which is a literal constant.
go to this page for a very elaborate & clear answer
http://home.netcom.com/~tjensen/ptr/ch6x.htm
In char a[]="abc",space is allocated for three characters and terminated with a NULL character.In char *a="abc",a is a pointer to the string.Hence in this declaration, a can be made to point to a different string whereas in the first declaration,a cannot be assigned to another string directly.It can be done only through strcpy...
Char a[] = "abc"; Here is is stored on stack.
- Asha February 15, 2009and sizeof a is 4.. It counts \0
char *a = "abc" --- a is stored on stack. But abc is a const string... Its stored in initialized data segment.