1

This is the point from ISO :Standard Conversions:Array-to-pointer conversion: $4.2.2

   A string literal (2.13.4) that is not a wide string literal can be converted 
    to an rvalue of type “pointer to char”; a wide string literal can be 
    converted to an rvalue of type “pointer to wchar_t”. In either case,
    the result is a pointer to the first element of the array. This conversion 
    is considered only when there is an explicit appropriate pointer target 
    type , and not when there is a general need to convert from an lvalue to
    an rvalue. [Note: this conversion is deprecated. ] 

   For the purpose of ranking in overload resolution (13.3.3.1.1), this 
   conversion is considered an array-to-pointer conversion followed by a
   qualification conversion (4.4). 

  [Example:"abc" is converted to "pointer to const char” as an array-to-pointer  
  conversion, and then to  “pointer to char” as a qualification conversion. ]

Can any one explain this, if possible with an example program.

I thing i know regarding string literals...may i know about above statement(wide string literal Prefix L usage).I know ..about the wide string literal meanig.But i need it according to the above satement I mean with Lvaue to Rvalue Conversions.

3 Answers3

1

Before const was introduced into C, many people wrote code like this:

char* p = "hello world";

Since writing to a string literal is undefined behavior, this dangerous conversion was deprecated. But since language changes shouldn't break existing code, this conversion wasn't deprecated immediately.

Using a pointer to a constant character is legal, since const correctness does not let you write through it:

const char* p = "hello world";

And that's all there really is too it. Ask specific questions if you need more information.

Community
  • 1
  • 1
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • 1
    Actually here my doubt is regarding WIDE STRING LITERAL.i mean You have to use the L prefix .I need the program expalation..which tell above point /statement –  Nov 19 '10 at 12:04
  • And in C++0x, the conversion will be removed altogether and will reject the broken code. So ppl should better change their broken code now. – Johannes Schaub - litb Nov 19 '10 at 14:03
0

When you write

cout<<*str    //output :s

it means that you obtain the str[0], as str is a char array and the pointer to array is the pointer to it's first element. str[0] appears to be a char, so cout as a clever object prints you what you wanted - the first character of your char array.

Also,

cout << (str+1)

will print 't'

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
0
   char* str = "stackoverflow";
   cout << str;  //output :stackoverflow

This is because the type of str is 'pointer-to-char', so the output is the complete zero-terminated string that starts at str.

  cout << *str    //output :s

Here, the type of *str is just char -- using the leading * dereferences the pointer and gived you back the 'thing-pointed-to', which is just the single char 's'.

bgporter
  • 35,114
  • 8
  • 59
  • 65