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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:22:54+00:00 2026-05-26T09:22:54+00:00

I know they are different, I know how they are different and I read

  • 0

I know they are different, I know how they are different and I read all questions I could find regarding char* vs char[]

But all those answers never tell when they should be used.

So my question is:

When do you use

const char *text = "text";

and when do you use

const char text[] = "text";

Is there any guideline or rule?

As an example, which one is better:

void withPointer()
{
    const char *sz = "hello";
    std::cout << sz << std::endl;
}

void withArray()
{
    const char sz[] = "hello";
    std::cout << sz << std::endl;
}

(I know std::string is also an option but I specifically want to know about char pointer/array)

  • 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-26T09:22:55+00:00Added an answer on May 26, 2026 at 9:22 am

    Both are distinctly different, For a start:

    1. The First creates a pointer.
    2. The second creates an array.

    Read on for more detailed explanation:

    The Array version:

    char text[] = "text"; 
    

    Creates an array that is large enough to hold the string literal “text”, including its NULL terminator. The array text is initialized with the string literal “text”.The array can be modified at a later time. Also, the array’s size is known even at compile time, so sizeof operator can be used to determine its size.


    The pointer version:

    char *text  = "text"; 
    

    Creates a pointer to point to a string literal “text”. This is faster than the array version, but string pointed by the pointer should not be changed, because it is located in an read only implementation defined memory. Modifying such an string literal results in Undefined Behavior.

    In fact C++03 deprecates use of string literal without the const keyword. So the declaration should be:

    const char*text = "text";
    

    Also,you need to use the strlen() function, and not sizeof to find size of the string since the sizeof operator will just give you the size of the pointer variable.


    Which version is better?

    Depends on the Usage.

    • If you do not need to make any changes to the string, use the pointer version.
    • If you intend to change the data, use the array version.

    EDIT: It was just brought to my notice(in comments) that the OP seeks difference between:

    const char text[] and const char* text

    Well the above differing points still apply except the one regarding modifying the string literal. With the const qualifier the array test is now an array containing elements of the type const char which implies they cannot be modified.

    Given that, I would choose the array version over the pointer version because the pointer can be(by mistake)easily reseated to another pointer and the string could be modified through that another pointer resulting in an UB.

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

Sidebar

Related Questions

I know they're using a jQuery plugin, but I can't seem to find which
I have read through the solutions to similar problems, but they all seem to
I know how they are different syntactically, and that C++ uses new, and C
What is the difference between those 2 files? Like I know they are for
I know they are bad practice but I have a real need on an
I'm thinking about frameworks. I know they make life a lot easier, but as
I hear people writing these programs all the time and I know what they
I've read all 484 pages of Professional XMPP and read countless forum threads regarding
I've read many of the existing questions/threads on this subject, but keep in mind
I have read through all the threads out there that looked as if they

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.