Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > initialisation of a char pointer using char *s = "something"

Reply
Thread Tools

initialisation of a char pointer using char *s = "something"

 
 
Keith Thompson
Guest
Posts: n/a
 
      03-12-2008
Ian Collins <(E-Mail Removed)> writes:
> Brice Rebsamen wrote:
>> Reading the code from showkey.c (from package kbd) I found this type
>> of code:
>>
>> char *m;
>> m = "RAW";
>>

> The character pointer m (which should probably be a const char*) points
> to the string literal "RAW".
>
>> See below for the complete code. How can this work? I would have used
>> strdup, or allocation of the memory for m (static or dynamic) then
>> strncpy.
>>

> Remember m it a pointer.


I suspect Brice's confusion is based not on the fact that m is a
pointer (it obviously is, since it's declared that way), but on the
fact that "RAW" isn't a pointer.

If you're unfamiliar with C, you know that "=" is an assignment
operator, and that it causes the value of the right hand side to be
copied to the object named by the left hand side. In this case, you
might assume that the characters 'R', 'A', and 'W' (and the trailing
'\0') are going to be copied -- but it ain't so.

A string literal is an array of char. The trick is that, like any
expression of array type, it's implicitly converted to a pointer to
the array's first element in most contexts. This conversion doesn't
happen when the array is the operand of a unary "&" or "sizeof"
operator, or when it's a string literal use to initialize an array.

(In case you were wondering, the last case doesn't apply here, since
(a) it's an assignment, not an initializer, and (b) the object being
initialized^H^H^H^H^H^H^H^H^H^H^H assigned to isn't an array.)

So the assignment
m = "RAW";
copies, not the string "RAW", but the address of its first character,
into m.

The relationship between arrays and pointers in C can be confusing,
and some features of the language almost seem to have been designed to
maintain that confusion. The best cure I know of is to read and
understand section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to restructure old C initialisation of char* to avoid compiler warnings tropos C++ 6 09-21-2006 07:03 AM
Initialisation of reference vs. initialisation of reference member Tim Clacy C++ 8 05-30-2006 06:14 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments