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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:57:05+00:00 2026-05-14T04:57:05+00:00

I have a requirement to create two different maps in C++. The Key is

  • 0

I have a requirement to create two different maps in C++. The Key is of type CHAR* and the Value is a pointer to a struct. I am filling 2 maps with these pairs, in separate iterations. After creating both maps I need find all such instances in which the value of the string referenced by the CHAR* are same.

For this I am using the following code :

typedef struct _STRUCTTYPE
{
.. 
} STRUCTTYPE, *PSTRUCTTYPE;

typedef pair <CHAR *,PSTRUCTTYPE> kvpair;

..

CHAR *xyz;

PSTRUCTTYPE abc;

// after filling the information;

Map.insert (kvpair(xyz,abc));


// the above is repeated x times for the first map, and y times for the second map.
// after both are filled out;

std::map<CHAR *, PSTRUCTTYPE>::iterator Iter,findIter;

for (Iter=iteratedMap->begin();Iter!=iteratedMap->end();mapIterator++)
{
  char *key = Iter->first;

  printf("%s\n",key);

  findIter=otherMap->find(key);

  //printf("%u",findIter->second);

  if (findIter!=otherMap->end())
  {
    printf("Match!\n");
  }
}

The above code does not show any match, although the list of keys in both maps show obvious matches. My understanding is that the equals operator for CHAR * just equates the memory address of the pointers.

My question is, what should i do to alter the equals operator for this type of key or could I use a different datatype for the string?

  • 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-14T04:57:06+00:00Added an answer on May 14, 2026 at 4:57 am

    My understanding is that the equals operator for CHAR* just equates the memory address of the pointers.

    Your understanding is correct.

    The easiest thing to do would be to use std::string as the key. That way you get comparisons for the actual string value working without much effort:

    std::map<std::string, PSTRUCTTYPE> m;
    PSTRUCTTYPE s = bar();
    m.insert(std::make_pair("foo", s));
    
    if(m.find("foo") != m.end()) {
        // works now
    }
    

    Note that you might leak memory for your structs if you don’t always delete them manually. If you can’t store by value, consider using smart pointers instead.

    Depending on your usecase, you don’t have to neccessarily store pointers to the structs:

    std::map<std::string, STRUCTTYPE> m;
    m.insert(std::make_pair("foo", STRUCTTYPE(whatever)));
    

    A final note: typedefing structs the way you are doing it is a C-ism, in C++ the following is sufficient:

    typedef struct STRUCTTYPE {
        // ...
    } *PSTRUCTTYPE;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement where i have create replication between two tables with different
I have a requirement to create a xaml page with Semantic Zoom where the
I have a requirement to create a background processor that works only when the
I have a requirement to create a windows form control which has to detect
I have a requirement to create a simple windows forms application that allows an
I have a requirement to create a multi level treeview in ASP.Net (with VB)
If I have a requirement to create a data structure that has the following
For a project we have a requirement to create an interfacedefinition that will return
I am new to MySQL coming from Oracle. I have a requirement to create
I have a requirement where the CMS admin will create the content [Article] page

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.