CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2021
    Posts
    6

    How to return the value of a const char* method

    Hey guys, I am just starting to learn C++ here, and I am a bit confused by this method:

    Code:
    extern const char* getOwnerSerNoString(void);
    which is defined as follows:

    Code:
    const char* getOwnerSerNoString(void)
    {
      const char* result = FileData[FILE_ID_TJ_INFO].OwnerSerNoStr;
    
      if ( !strcmp( result, "" ) )
      {
        result = FileData[FILE_ID_LH_INFO].OwnerSerNoStr;
      }
    
      return result;
    }
    it seems to be returning a char[] which I believe in C++ is just a string, correct? So, if I want to get the full string itself, and convert it to an actual string using
    Code:
    #include <string>
    what would that look like? Do I need a loop for that?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to return the value of a const char* method

    Code:
    std::string myString = getOwnerSerNoString();
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2021
    Posts
    6

    Re: How to return the value of a const char* method

    Okay, so I think I get it now. When you have a * it's sort of just a way of pointing at the original data rather than re-declaring it. I thought maybe I had to use the & operator somehow but I guess that's if I want to access the location in memory where the data is stored.

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