Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16949

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

Report
one question Posted by svaz on 26 Jan 2006 at 11:06 PM
what is the difference between char name[] and char *name:
can you please explain me with an example?
Report
Re: one question Posted by Donotalo on 27 Jan 2006 at 1:20 AM
: what is the difference between char name[] and char *name:
: can you please explain me with an example?
:

it depends how u r using name[] and *name; in the following context, these two are same:
char name[] = "First name";
--------------------------
char *name = "First name";

in the declaration in function prototype, these two are also same:
void function(char *name);
---------------------------
void function(char name[]);

however, usually when char name[] is used, it means a static array, whose address cannot be changed. but when char *name is used, it can be point to any valid memory of the same type (ie, char here). look at the following code, where u cannot use [] and * interchangeably:
char name[] = "The string";
char *ptr;
for (ptr = name; *ptr; ptr++)
    cout << *ptr << '-';

also, u cannot use pointer arithmatic with name, where as it is possible with ptr (as u see, ptr++).


~Donotalo()




 

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.