Reputation: 26
string a= "Stack Overflow";
char b[]= "Stack Overflow";
cout<<sizeof(a)<<","<<sizeof(b)<<endl;
Output of above code is 4,15
Since 'a' points to the string, it has size 4 that of a string on my machine.
Upvotes: 0
Views: 100
Reputation: 206567
Since 'a' points to the string, it has size 4 that of a string on my machine.
Not exactly.
a
IS A string. It is not a pointer and, hence, does not point to a string. The implementation of string
on your setup is such that sizeof(string)
is 4.
'b' is also pointer to string, but why it has size of 15 (i.e. of sizeof("Stack Overflow")) ?
Not true.
b
is not a pointer to a string. It is an array of char
. The line
char b[]= "Stack Overflow";
is equivalent to:
char b[15]= "Stack Overflow";
The compiler deduces the size of the array and creates an array of the right size.
Upvotes: 6