Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7572381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:54:43+00:00 2026-05-30T15:54:43+00:00

I am trying to copy a string which might contain null characters in the

  • 0

I am trying to copy a string which might contain null characters in the middle to a char array.
I have constructed the following function.

 void SaveStringToChar(string &mystring,const char * &ArrChar)
 {//begin function

   std::string str;
   char * writable = new char[str.size() + 1];
   std::copy(str.begin(), str.end(), writable);
   writable[str.size()] = '\0';
   ArrChar = writable;

 }//end function

my question is this method guarantees that I wont lose the characters after a null element.
and my other question is I get this linker error which I don’t know what it means.

/tmp/ccUpCRaz.o: In function `Parser::RuleParser(char const*)’:
Parser.cpp:(.text+0x3f6): undefined reference to Parser::SaveStringToChar(std::basic_string, std::allocator >&, char const*&)’
collect2: ld returned 1 exit status

anyhint please.

this is the function I pass things to compare.

  void Search( size_t TextLength, const char *Text, const vector<const char *> &patterns );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:54:44+00:00Added an answer on May 30, 2026 at 3:54 pm

    You need to define the function as a member of Parser::. It is currently defined as:

    void SaveStringToChar(string &mystring,const char * &ArrChar)
    

    Change to:

    void Parser::SaveStringToChar(string &mystring,const char * &ArrChar)
    

    The std::copy() will copy all the characters. However, the caller will not know how many characters ArrChar actually points to as functions like strlen() will stop counting at the first null character. You could change the signature of SaveStringToChar() to accept another argument size_t& ArrCharLen that would be populated with the number of characters in ArrChar. Of course, mystring will have the length of ArrChar so maybe you are already storing this prior to the call to SaveStringToChar().

    EDIT (after vector<const char*> comment):

    The true number of bytes is lost once the const char* is added to the vector. Why not use a vector<std::string> and you have std::string::length() to know how many characters there are actually stored in std::string.

    EDIT2 (after comparison function .c_str() with text.length():

    Instead of passing .c_str() and .length() to compare against elements in a vector<const char*> just pass the std::string to the compare function and compare against elements of a vector<std::string>: the std::string::operator==() will correctly compare strings will embedded null characters. You could then use std::find() to search the vector<std::string> for the std::string read from the file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to compress any given string to a shorter version, copy paste-able compressed
Yo! I'm trying to copy a few chars from a char[] to a char*.
I have a list of files that I'm trying to copy and move (using
I'm trying to make use of a function which comes in a 3rd party
I have the following code which we use to compress Strings (with error and
i am trying to make help for my application. I have xps documents which
I have the following code which throws an exception (detail in code comments below).
I'm trying to write a Linq query which returns an array of objects, with
I am trying to write a copy method which can be used to copy
I am trying to use a ostringstream to build a string which uses the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.