C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28651
Number of posts: 94667

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Declaring Strings Posted by tubby on 6 May 2011 at 2:23 AM
I can write char a[] = "hello world"; , but i can not write
char a[];
a = "hello world";
why so?
Report
Re: Declaring Strings Posted by pseudocoder on 6 May 2011 at 10:30 AM
char a[] = "hello world";

you can that because the compiler knows how much space to allocate for the string "hello world."

char a[];
a = "hello world";

C won't allow aggregate assignment; you've got an array of chars and a string, which aren't exactly the same thing. you've also got an unknown size - the compiler doesn't know much space to allocate to a.

char a[80];
strcpy(a, "hello world");

An interesting feature of C though is you can assign structs.

typedef struct {
   char a[40];
} test;

test t1 = { "hello world" }, t2 = t1;

puts(t1.a);
puts(t2.a);


HTH



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.