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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:01:59+00:00 2026-05-26T08:01:59+00:00

I am getting following error error: invalid conversion from ‘const int*’ to ‘int*’ Following

  • 0

I am getting following error error: invalid conversion from ‘const int*’ to ‘int*’
Following is my program

#include <set>
int main ( int argc, char **argv) {
    std::set<int> intSet;
    intSet.insert(1);
    intSet.insert(2);
    intSet.insert(3);
    intSet.insert(4);
    intSet.insert(5);

    int *pAddress = &(*(intSet.find(4)));
}

I want address of the element in the std::set , This code does not give any compilation error with Microsoft compiler but g++ is giving this compilation error.

  • 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-26T08:02:00+00:00Added an answer on May 26, 2026 at 8:02 am

    It is because each element of std::set is stored as T const, and the implementation has a reason to do so.

    Since std::set contains exactly a single copy of a value. It has to make it immutable, otherwise, one can change it’s value to something which already exists in the set.

    Even if the elements were mutable, you would not be able the change the value, because std::set::find is a const member function, and therefore in this function intSet is immutable and effectively each elements would become immutable as well, including the iterator which it returns, and through which you may change the value at the call-site.

    The only want to take the address is this:

    int const *paddress =  &(*(intSet.find(4))); //const int* is same as int const*
    

    Don’t use const_cast though,

    //DANGEROUS - DONT DO IT
    int *paddress =  const_cast<int*>(&(*(intSet.find(4)))); //will compile, 
                                                           //but it is dangerous!
    
    //you might accidentally write
    *paddress = 10; //undefined behaviour, 
                    //though the compiler will let you write this
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting the following error when trying to build: error: invalid initialization of non-const
Not sure why I'm getting this error. I have the following: int* arr =
I am getting following error when i am trying to connect Production DB from
I'm getting the following error from g++ while trying to add iterator support for
With a MS SQL Stored Procedure I am getting the following error Invalid Column
I am getting the following error when I post back a page from the
I am getting the following error message from a bulk load in sql server
Can anyone see from the following function why I would be getting an Invalid
When i run junit test cases, getting following error: Invalid bean definition with name
I'm getting the following InvalidOperationException: The parameter conversion from type 'System.String' to type 'System.Runtime.Serialization.ExtensionDataObject'

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.