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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:40:10+00:00 2026-06-04T14:40:10+00:00

I have the following code char ptr=new char(); int counter = 1; string s

  • 0

I have the following code

 char ptr=new char();


 int counter = 1;
 string s = new System.String(ptr, counter);

 // does not show something
 MessageBox.Show(s+"Something");

 //shows something
 MessageBox.Show("Something" + s);

The first Messagebox shows nothing enter image description here

The Second Messagebox shows something enter image description here

If the counter value is 0 then both messagebox shows same result but if counter is greater than 0 then the problem occurs.

I think the problem is with new string(ptr, counter) initilization

. But I want to know the internal mechnism why this is occured.

  • 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-06-04T14:40:11+00:00Added an answer on June 4, 2026 at 2:40 pm

    ptr is a null character ('\0') and s is a string with one copy of that character (i.e. "\0"). So, at runtime, your first call’s parameter evaluates to "\0Something" whereas your other one evaluates to "Something\0".

    In C#, strings are allowed to have null characters; you can conceptualize them as just a char[] array (which has a known length); So null characters are OK. The issue comes about when you pass to a C API. C doesn’t have strings, so they’re immitated using null-terminated strings. As far as any C API is concerned, "\0Something" is an empty string (strlen would return 0). So, when you use MessageBox.Show, your string is passed on down to the Win32 API function, MessageBoxW which only understands null-terminated strings.


    From the .NET source code for MessageBox.Show(string)

    public static DialogResult Show(string text)
    {
        return ShowCore(null, text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, 0, false);
    }
    

    Towards the end of ShowCore, we see this line:

    return (DialogResult)MessageBoxW(handle, text, caption, style);
    

    And that MessageBoxW call is that Win32 API function in user32.dll.

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

Sidebar

Related Questions

I have the following code: #include <string.h> int main(void) { char *buffer = NULL,
i have the following code below: const char* timeformat = %Y-%m-%d %H:%M:%S; const int
I have the following code int ParseData(unsigned char *packet, int len) { struct ethhdr
I have the following code: tmp_data = simulated_data[index_data]; unsigned char *dem_content_buff; dem_content_buff = new
I have the following code: int main(int argc, char** argv) { onelog a; std::cout
Suppose I have the following code: void* my_alloc (size_t size) { return new char
I have the following code: long fp = ... void (*ptr)(long, char*, char*) =
I have the following code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <curl/curl.h> char
I have the following C code: static void* heap; static unsigned int ptr; int
I have the following code: int main() { char *sPPhrase[51]; /* Input */ printf(Enter

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.