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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:05:31+00:00 2026-06-04T15:05:31+00:00

So I did the following test: char* a = test; char* b = test;

  • 0

So I did the following test:

char* a = "test";
char* b = "test";
char* c = "test\0";

And now the questions:

1) Is it guaranteed that a==b? I know I’m comparing addresses. This is not meant to compare the strings, but whether identical string literals are stored in a single memory location

2) Why doesn’t a==c? Shouldn’t the compiler be able to see that they’re referring to the same string?

3) Is an extra \0 appended at the end of c, even though it already contains one?

I didn’t want to ask 3 different questions for this because they seem somehow related, sorry ’bout that.

Note: The tag is correct, I’m interested in C++. (although please specify if the behavior is different for C)

  • 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-04T15:05:32+00:00Added an answer on June 4, 2026 at 3:05 pm

    Is it guaranteed that a==b?

    No. But it is allowed by §2.14.5/12:

    Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation-defined. The effect of attempting to modify a string literal is undefined.

    And as you can see from that last sentence using char* instead of char const* is a recipe for trouble (and your compiler should be rejecting it; make sure you have warnings enabled and high conformance levels selected).

    Why doesn’t a==c? Shouldn’t the compiler be able to see that they’re referring to the same string?

    No, they’re not required to be referring to same array of characters. One has five elements, the other six. An implementation could store the two in overlapping storage, but that’s not required.

    Is an extra \0 appended at the end of c, even though it already contains one?

    Yes.

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

Sidebar

Related Questions

I want to pass tests which get the following: This test did not perform
Did I not get enough sleep or what? This following code var frame=document.getElementById(viewer); frame.width=100;
I was a little surprised that the following code did not work as expected:
I did the following: Create a working Android project (not a library or test
I did the following but it not work for always.It works if i launch
How can I create a const boost matrix? The following did not work: const
Consider the following code (for simplicity, I did not follow any C# coding rules).
Hopefully this is a very simple question. Following is the C pgm (test.c) I
I have this following test code: public static final String[] list = { apple,ball,cat,dog,egg,fan,girl,hat,igloo,jerk
So in order accomplish what I asked in this post I did the following:

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.