CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: char* name

  1. #1
    Join Date
    Dec 2008
    Posts
    29

    char* name

    Hi,

    I am a newbie to c++ and I am confused when to use char* name. Can someone please explain me when to use char*

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: char* name

    Have you learned pointers yet? If not, it's easiest to think of a char* as a char array of unspecified length. (However, simply declaring a char* will *not* get you that array----you have to ask for it. Common mistake.)

    A char* is often called a "C-style string", since it's usually used as the pointer to the first letter of a NULL-terminated character array representing a string. However, in C++ it's far easier and less error-prone to simply use a std::string instead of a char*.

  3. #3
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: char* name

    Use char* when you require a pointer to a char.
    Code:
    // Single char.
    char c;
    char *pc = &c;
    
    // Array of char.
    char a[10];
    pc = &a[0]; // Pointer to the first element in the array.
    pc = a; // Same as above.
    
    // Dynamically allocated.
    pc = new char [10];
    delete pc;
    Don't use char arrays for strings in C++, use std::string instead.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  4. #4
    Join Date
    Dec 2008
    Posts
    29

    Re: char* name

    Thanks Lindley,

    I am well versed with pointers but I got confused between the different style of declaring pointer
    as
    char* b
    char * b
    char *b

    Can you please explain me the difference between the three too....
    and how can we use std::string in a c++ programme

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: char* name

    There is no difference between those three things. And std::string is very intuitive to use, but here's a reference:
    http://www.cplusplus.com/reference/string/string/

  6. #6
    Join Date
    Dec 2008
    Posts
    29

    Question Re: char* name

    Hey,

    I took your advise and started writing a program using std::string it will not only save my program size with also save dynamic allocation problem of using char*, but the document I got does not inform me what lib file should I use and I will declare sub::string in the header file....so what should be the lib file.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: char* name

    std::string is part of the C++ standard library. You need to #include <string>.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: char* name

    All you need to do is #include <string> and (optionally) add
    Code:
    using std::string;
    //or
    using namespace std;
    to your cpp file. (Don't put using statements in h files, though.) Either of these will allow you to simply declare "string name" rather than "std::string name".

  9. #9
    Join Date
    Dec 2008
    Posts
    29

    Re: char* name

    Hi,

    I am still getting confused maybe since I am seeing a code after so many days maybe that is why
    can someone please explain me public, private, protected declaration and it is private declaration be default.

    What is int virtual

    Code:
    int virtual okSegment(Segment *t);
    Last edited by Shapy_shockerz; January 5th, 2009 at 12:56 PM.

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: char* name

    Quote Originally Posted by Shapy_shockerz View Post
    Hi,

    I am still getting confused maybe since I am seeing a code after so many days maybe that is why
    can someone please explain me public, private, protected declaration and it is private declaration be default.

    What is int virtual

    Code:
    int virtual okSegment(Segment *t);
    Don't confuse this board with documentation. MSDN and dozens of books all explain private, protected, public and virtual. RTFM!!!

  11. #11
    Join Date
    Dec 2008
    Posts
    29

    Lightbulb Re: char* name

    I know there are documentation on all this but I need hand on experience on this too..
    Please someone help me out with how to use atoi() and what it is?

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: char* name

    Quote Originally Posted by Shapy_shockerz View Post
    I know there are documentation on all this but I need hand on experience on this too..
    Please someone help me out with how to use atoi() and what it is?
    Then go get a teach yourself C++ book and work through it. We're not here to teach you the language. We're here to help when you get stuck. That means you take a shot at it and we'll help you find where you went wrong.

    As to atoi, go look it up and ask specific questions about what you don't understand.

  14. #14
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: char* name

    Quote Originally Posted by Shapy_shockerz View Post
    I know there are documentation on all this but I need hand on experience on this too..
    It is virtually impossible to learn C++ taking the "random tidbits" approach you are using. There is only one way to reliably learn C++

    1) Get a GOOD tutorial oriented book. The paper kind.
    2) Read every word, starting at page 1. No sknimming, skipping, browsing.
    3) TYPE in every line of code. Even if there is a DVD/CD, MANUALLY type it in.
    4) Step through EVERY line of code with your debugger (yes, even the trivial hello world sample)
    5) Do NOT proceed past the end of a chapter until you are confident that you understand, and retained the information.
    6) Write at least one program from scratch using the topic of the current chapter. Be sure to carefully step through every line with the debugger.

    C++ is a very detailed language, and attempting to pick things up as you go will (at best) leave holes in your knowedge, and (typically) have you make bad assumptions that are later nearly impossible to unlearn.

    I hate to count the number of times someone comes back with a problem about a very basic item, when told that it was in chapter 2 or 3 of the book that they did you, the response is "I skipped that because I already knew all about....."....OBVIOUSLY they did not.

    Once you get a good book, and begin carefully studying the material, posting problems you may encounter can be very effective. By stating the book and page you are up to, people can phrase the response in a manner that should be understandable to a person who has carefully studied, and comprehended all of the material up to that point.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  15. #15
    Join Date
    Dec 2008
    Posts
    29

    Re: char* name

    Hi

    I am reading a code and it says to read a file we need the following command as it makes a matrix to read the code, I am not sure why is it atoi(argv[2])
    Code:
     n = atoi(argv[2]);              
       strncpy(&option,argv[4],1);
    i am struck here please guide me as in wht this line would do

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured