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

  • Home
  • SEARCH
  • 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 3991828
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:39:46+00:00 2026-05-20T06:39:46+00:00

char* pStr = new String(Hello); char* s = Hello; Is the first one correct?

  • 0
char* pStr = new String("Hello");
char* s = "Hello";

Is the first one correct? Are there any difference between these two? My guess is that the first one is allocated on the heap,and the other one an the stack.Am i correct or are there any other differences?

  • 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-20T06:39:46+00:00Added an answer on May 20, 2026 at 6:39 am

    The first one is just incorrect and won’t compile because there is no such thing as String in either C or C++. The second one will compile, and is fine in C(afaik). In C++, however, the conversion from a string literal to char* is deprecated. You can unintentionally write later s[0] = 'X'; which is undefined behavior.

    The correct way of doing it is using const (in C++)

    const char * s = "Hello";
    

    or, better, use string

    std::string s("Hello"); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

char c='c'; int i=10; double d =50; long l=30; String s=Goodbye; Are these statement
char first[10]; char second[10]; What does fscanf(fr,%s %s\n,first,second); do when first string is too
char *str = malloc (14); sprintf(str, %s, one|two|three); char *token1, *token2, *token3; char *start
char *strtok(char *s1, const char *s2) repeated calls to this function break string s1
char* p = hello world; programmers usually assigns the address of an variable to
char *s = hello ppl.; for (i = 0; i < strlen(s); i++) {
char testStr[] = trim this ; char** pTestStr = &testStr; trim(pTestStr); int trim(char** pStr)
char character = 'c'; string str = null; str = character.ToString();//this is ok char[]
char *pointer1; char *pointer2; pointer1 = new char[256]; pointer2 = pointer1; delete [] pointer1;
char *sample = String Value; &sample is a pointer to the pointer of String

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.