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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:51:38+00:00 2026-05-26T01:51:38+00:00

If an input const string is being modified in some way (which is resulting

  • 0

If an input const string is being modified in some way (which is resulting in C compiler warning), what is the best way to handle it – typecasting it to a new variable and then using it OR duplicating it and using it and then freeing it. Or is there any other way to handle this type of scenario. please suggest. Any help would be appreciated.

//Typecasting

const char * s1;
char * s2 = (char *)s1;

//Duplicate and free

const char * s1;
char * s2  = strdup( s1 );
free(s2)

EDIT: It is a C compiler; not C++. I am not sure whether in typecasting, s2 will be a new copy of string s1 or will it be pointing to the original string s1?

Thanks for the answers. I have one more doubt-

const char * c1;
const char * c2 = c1;

Is the above assignment valid?

  • 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-26T01:51:39+00:00Added an answer on May 26, 2026 at 1:51 am

    Casting away const here might shut the compiler up but will lead to runtime failures. Make a copy of the string and work on that.

    Casting away const does not copy the contents of the memory. It just creates a pointer to the same memory and tells the compiler that it can go right ahead and write to that memory. If the memory is read only you have a protection fault. More seriously you can have correctness problems which are hard to debug. Don’t cast away const.

    Of course, if you need to modify a variable and have those modifications visible to the caller, then you should not make it const in the first place. On the other hand, if the modifications are meant to be private to the function, then duplication of the const parameter is best.

    As a broad rule you should attempt to avoid casts if at all possible. Casts are one of the most frequent sources of errors.

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

Sidebar

Related Questions

Given the following input and regex strings: const string inputString = ${Principal}*${Rate}*${Years}; const string
The following test code: [Test] public void PossibleHtmlAgilityPackBug() { const string html = @<input
Input: 1) A huge sorted array of string SA; 2) A prefix string P;
As input I get an int (well, actually a string I should convert to
I have a list of ~20,000 email addresses, some of which I know to
float Calculate(const string &query) { std::cout << Query: << query << \n; unsigned int
Every time I need to design a new database I spend quite some time
as per the class reference the TixmlText takes const char* values as input. i
Need some help for creating a File and String search engine. The program needs
public static Int64 Decode(string input) { var reversed = input.ToLower().Reverse(); long result = 0;

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.