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 794027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:14:57+00:00 2026-05-14T22:14:57+00:00

For regular C strings, a null character ‘\0’ signifies the end of data. What

  • 0

For regular C strings, a null character '\0' signifies the end of data.

What about std::string, can I have a string with embedded null characters?

  • 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-14T22:14:58+00:00Added an answer on May 14, 2026 at 10:14 pm

    Yes you can have embedded nulls in your std::string.

    Example:

    std::string s;
    s.push_back('\0');
    s.push_back('a');
    assert(s.length() == 2);
    

    Note: std::string‘s c_str() member will always append a null character to the returned char buffer; However, std::string‘s data() member may or may not append a null character to the returned char buffer.

    Be careful of operator+=

    One thing to look out for is to not use operator+= with a char* on the RHS. It will only add up until the null character.

    For example:

    std::string s = "hello";
    s += "\0world";
    assert(s.length() == 5);
    

    The correct way:

    std::string s = "hello";
    s += std::string("\0world", 6);
    assert(s.length() == 11);
    

    Storing binary data more common to use std::vector

    Generally it’s more common to use std::vector to store arbitrary binary data.

    std::vector<char> buf;
    buf.resize(1024);
    char *p = &buf.front();
    

    It is probably more common since std::string‘s data() and c_str() members return const pointers so the memory is not modifiable. with &buf.front() you are free to modify the contents of the buffer directly.

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

Sidebar

Related Questions

I'm building a hashmap class that can have string keys and ints, bools, strings
Regular Expressions are usually expressed as strings, but they also have properties (ie. single
I want to use regular expression to find strings in a file that have
I have this Regular Expression that matches the following strings: <!-- 09-02-2009 ---> <!--
I have a set of strings. I would like to extract a regular expression
I have strings like: http://127.0.0.1:22/Test Is there some single function maybe using regular expressions
Problem I'm having trouble with a regular expression. I have a set of strings.
I'd like a JavaScript regular expression that can match a string either at the
I have to parse out color information from HTML data. The colors can either
I have a few questions I would like to ask about string literals and

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.