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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:35:23+00:00 2026-05-29T04:35:23+00:00

It can be simple but I m new about c++ In char arrays we

  • 0

It can be simple but I m new about c++
In char arrays we can let the compiler to count number of characters in the string like

char myarray[]="stringvar";

its ok, but if i change the code as below,the compiler gives error

string myvar = "stringvar";
char myarray[] =myvar;

error: initializer fails to determine size of myarray

Why is that?

  • 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-29T04:35:25+00:00Added an answer on May 29, 2026 at 4:35 am

    You can do this:

    string myvar = "stringvar";
    const char * myarray = myvar.c_str(); //immutable string
    

    In this case, the data whichmyarray points to, lives as long as the lifetime of myvar.

    However, if you want a mutable string or, a string which may last longer (or shorter) than the lifetime of myvar, then you’ve to allocate memory yourself as:

    char * myarray = new char[myvar.size()+1]; //mutable string
    std::strcpy(myarray, myvar.c_str());
    
    //you've to deallocate the memory yourself as:
    delete [] myarray;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like it should be very simple but I can't get it to
This seems simple but I can't figure it out. I receive post data in
This should be totally simple but I can't get it working no matter what
This must be quite simple but I can't seem to figure this out. I've
The question look simple but i can't find any information on Firebreath's wiki page
I'm sure this is simple but I can't figure out how to achieve it:
I'm trying to do something thats fairly simple but I can't see the best
Hey guys, this is simple but I can't make it workn... blah! I have
Sorry, I'm sure this is simple but I'm tired and can't figure it out.
This is probably a simple question but I can't seem to figure out how

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.