Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > char array Q

Reply
Thread Tools

char array Q

 
 
suresh shenoy
Guest
Posts: n/a
 
      02-20-2008
I know that
char s[100];
s = "xyz";
is invalid and should use char ptr or string library to store a string
literal in s. Can anyone be precise why the second line is invalid?

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      02-20-2008
suresh shenoy said:

> I know that
> char s[100];
> s = "xyz";
> is invalid and should use char ptr or string library to store a string
> literal in s. Can anyone be precise why the second line is invalid?


The assignment operator requires as its left operand a modifiable lvalue.
Arrays are not modifiable lvalues.

Initialisation with a string literal is, however, permissible:

char s[100] = "xyz";

The contents of s are now 'x', 'y', 'z', and 97 '\0's.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      02-20-2008
On Feb 20, 1:42 pm, Richard Heathfield <(E-Mail Removed)> wrote:
> suresh shenoy said:
>
> > I know that
> > char s[100];
> > s = "xyz";
> > is invalid and should use char ptr or string library to store a string
> > literal in s. Can anyone be precise why the second line is invalid?

> char s[100] = "xyz";
>
> The contents of s are now 'x', 'y', 'z', and 97 '\0's.


Just wanted to mention why this happends;
In array initialization, when the initialization does not initialize
all the elements of the array, the rest are given the value 0 (or NULL
in pointer context)
Notice, I said the rest; which means int foo[3] = { [1] = 42 };
guarantees foo[0] to be 0.
 
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
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 6 09-01-2006 05:57 PM
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 0 09-01-2006 03:22 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
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 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



Advertisments