Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: char * vs char[]

Reply
Thread Tools

Re: char * vs char[]

 
 
John Harrison
Guest
Posts: n/a
 
      06-25-2003

"C Wood" <> wrote in message
news:LDlKa.12831$...
>
> Dear group,
>
> I forgot which declaration is constant. Intentions are:
>
> void some_func() {
> char temp[] = "prepend"; /*This one*/
> char *temp= "prepend"; /*Or this one*/
> strcat(temp," before this");
> }
>
> Thanks...
>


The second, because all you've declared is a pointer which points at a
string literal, and string literals are read-only.

Despite appearances the first doesn't involve a string literal, its a
confusing short hand for this.

char temp[] = {'p', 'r', 'e', 'p', 'e', 'n', 'd', '\0' };

Obviously if you declare a non-const array, you can modify it.

Your strcat is illegal in both cases, nothing to do with const, instead you
are writing over memory you haven't allocated in any way.

john


 
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
(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
char *fred; char * fred; char *fred; any difference? Ben Pfaff C Programming 5 01-17-2004 07:37 PM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57