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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:28:29+00:00 2026-05-27T16:28:29+00:00

A little context: I’m trying to make a very simple hashing function/hash table as

  • 0

A little context: I’m trying to make a very simple hashing function/hash table as described here. I’m basically on the first step, blindly adding a key to an array based on the letter it starts with (no checking if space is occupied yet). The code I’m using to do this so far:

int main(int argc, char **argv) {
    char *arrayKeys[300];
    std::string aName("Charles");

    char *aNameCpy = new char[aName.size() + 1];
    std::copy(aName.begin(), aName.end(), aNameCpy);
    aNameCpy[aName.size()] = '\0';

    int kPos = storeKey(arrayKeys, aNameCpy);

    std::cout << "The new position in arrayKeys for 'Charles' is:  " <<
        kPos << "\ncontaining the text: " << arrayKeys[kPos] << std::endl;
    delete[] aNameCpy;  
    return 0;
}

int storeKey(char **keys, char *key) {

    int charLett = -1;
    charLett = (int)key[0];
    if(charLett != -1)
        charLett = charLett - 65;

    keys[charLett * 10] = key;

    return charLett*10;
}

My question is, how can I add a string to the array (arrayKeys) where it is fully apart of the array, and not reliant upon the original string? If I delete the copy of the string (aNamCpy) before I print the array key out, the array key turns into garbled symbols. I’m copying the string before sending it to the function because I need a non-const string to add to the arrayKeys array (so it can be modified), and any method of string I looked at seemed to return const.

(Another version of this I attempted can be found here, but I would rather not initialize the arrayKeys like that – with a definite second dimension (string) length)
C++ is still very new to me so I can’t figure out how to juggle the non-const part with copying the string into arrayKeys. Any help would be very much appreciated.

  • 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-27T16:28:30+00:00Added an answer on May 27, 2026 at 4:28 pm

    Here’s how I’d change the code to use more modern C++ constructs. I think you’ll find this way easier to use.

    int storeKey(vector<string> &keys, const string &key) {
        int charLett = -1;
    
        if (!key.empty()) {  // you weren't doing this before!
            charLett = key[0];
            charLett = toupper(charLett) - 'A';
            keys[charLett * 10] = key;
        }
    
        return charLett*10;
    }
    
    int main() {
        vector<string> arrayKeys(300);
        std::string aName("Charles");
    
        // No need to bother with the awkward copying.
        // std::vector and std::string will take care of it for us.
    
        int kPos = storeKey(arrayKeys, aName);
    
        if (kPos >= 0) {
            cout << "The new position in arrayKeys for 'Charles' is:  " <<
                kPos << "\ncontaining the text: " << arrayKeys[kPos] << endl;
        }
    
        // Don't have to remember to delete anything because nothing was new'ed.
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Skip to bottom for question, but first, a little context. So I have been
First, a little bit of context to explain why I am on the UDP
I have first to explain a little bit my context, then the question: I
I have a little problem with the context.fillText() function. When I call a function
I'm writing a little shortcut function to basically do the same thing as direct_to_template
For a little context, i'm trying to solve Project Euler problem 31 using excellent
To first give a little bit of context, my use-case is that my partner
A little context first: I would say I have good SQL server experience, but
So to give a little context, my goal here is to produce a binary
My little site should be pooling list of items from a table using the

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.