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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:08:41+00:00 2026-06-15T15:08:41+00:00

I keep getting the error Conversion from string literal to char* is deprecated in

  • 0

I keep getting the error “Conversion from string literal to char* is deprecated” in my code. The purpose of the code is to use a pointer-to-pointer to assign string1 and string2 a word, then print it out. How can I fix this?

Here is my code:

#include <iostream>
using namespace std;

struct WORDBLOCK
{
    char* string1;
    char* string2;
};

void f3()
{
    WORDBLOCK word;

    word.string1 = "Test1";
    word.string2 = "Test2";


    char *test1 = word.string1;
    char *test2 = word.string2;

    char** teststrings;

    teststrings = &test1;
    *teststrings = test2;

    cout << "The first string is: "
         << teststrings
         << " and your second string is: "
         << *teststrings
         << endl;  
}
  • 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-15T15:08:41+00:00Added an answer on June 15, 2026 at 3:08 pm

    C++ string literals are arrays of const char, which means you can’t legally modify them.

    If you want to safely assign a string literal to a pointer (which involves an implicit array-to-pointer conversion), you need to declare the target pointer as const char*, not just as char*.

    Here’s a version of your code that compiles without warnings:

    #include <iostream>
    
    using namespace std;
    
    struct WORDBLOCK
    {
        const char* string1;
        const char* string2;
    };
    
    void f3()
    {
        WORDBLOCK word;
    
        word.string1 = "Test1";
        word.string2 = "Test2";
    
        const char *test1 = word.string1;
        const char *test2 = word.string2;
    
        const char** teststrings;
    
        teststrings = &test1;
        *teststrings = test2;
    
        cout << "The first string is: "
             << teststrings
             << " and your second string is: "
             << *teststrings
             << endl;
    }
    

    Consider what could happen if the language didn’t impose this restriction:

    #include <iostream>
    int main() {
        char *ptr = "some literal";  // This is invalid
        *ptr = 'S';
        std::cout << ptr << "\n";
    }
    

    A (non-const) char* lets you modify the data that the pointer points to. If you could assign a string literal (implicitly converted to a pointer to the first character of the string) to a plain char*, you’d be able to use that pointer to modify the string literal with no warnings from the compiler. The invalid code above, if it worked, would print

    Some literal
    

    — and it might actually do so on some systems. On my system, though, it dies with a segmentation fault because it attempts to write to read-only memory (not physical ROM, but memory that’s been marked as read-only by the operating system).

    (An aside: C’s rules for string literals are different from C++’s rules. In C, a string literal is an array of char, not an array of const char — but attempting to modify it has undefined behavior. This means that in C you can legally write char *s = "hello"; s[0] = 'H';, and the compiler won’t necessarily complain — but the program is likely to die with a segmentation fault when you run it. This was done to maintain backward compatibility with C code written before the const keyword was introduced. C++ had const from the very beginning, so this particular compromise wasn’t necessary.)

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

Sidebar

Related Questions

I keep getting the error Conversion from string to type 'Double' is not valid
With the following code, I keep getting error C2535 when I compile. It's complaining
I'm trying to code some stuff for a game but I keep getting error
I keep getting this error on emulator: I copied this from a tutorial and
As the title says, keep getting this error when trying to compile. From Googling
Trying to run a query, keep getting ERROR 1054 : SELECT * from my_table
I keep getting error in my iPhone programing when I try to use pi.
I keep getting this annoying message (even though it is my fault): Conversion from
I'm trying to install a package that uses global-linum-mode and keep getting: error Autoloading
Keep getting this error after inserting a subdatasheet into a query and trying to

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.