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

The Archive Base Latest Questions

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

I’m trying to write a little CLI Hangman game, just to create something in

  • 0

I’m trying to write a little CLI Hangman game, just to create something in C using my very limited set of language features, making the information so far stick.

I haven’t got to strings in the book yet, so I’ve created a so-called dictionary, which is a multi-dimensional array of characters, each row representing a word, and each column a letter. Therefore, if I want to put today’s Dictionary.com word of the day (“prognosticate”) in the dictionary, I write something like

dictionary[MAX_WORDS][MAX_CHARS] = {
     ...
     {'p', 'r', 'o', 'g', 'n', 'o', 's', 't', 'i', 'c', 'a', 't', 'e'},
     ...
}

Next, I represent the word to be printed on screen as an array of characters, initially made of underscores.

The way I thought of it, when the player enter a letter, I check to see if it exists within the current word. If it does, I replace the underscores in the word[] array with the actual letter. I consider the word to be guessed when there are no underscores left in the word[] array.

The first thought was to write a function int in_array(char array[], char letter) which would return 0 or 1 based on whether the letter is found in the array. But then I figured out I couldn’t pass it dictionary[][] as the first argument. I haven’t figured out a solution so far, so I’ll have to either use two functions, one for each array, or… rethink the whole idea.

So to sum this up, I need a function to check whether an element exists within either a one-dimensional or multi-dimensional array. Any solutions? Thanks in advance!

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

    You can pass the entire dictionary via the somewhat janky

    void foo(char dict[][MAX_CHARS], int whichWord);
    

    i.e. by telling the compiler exactly how big each row of the 2D array is in this case, and which row you want as a separate parameter. Then you use your usual 2-subscript notation to access elements.

    To pass one row of this array is easy too. Let’s say you’re doing guesses on the second word:

    void foo(char[] word, int numChars);
    
    foo(&(dict[1][0]), MAX_CHARS);
    

    which might be little ahead of where you’re at, but says “take the address in memory of the first character in the row I want, the second row (remember: 0-based indexing!), and treat it as the start of a one-dimensional array.”

    The problem you need to solve with this approach is telling the routine how many characters are in your word. If you use MAX_CHARS you have a problem, because presumably not every word is that length, and the characters you don’t assign are uninitialized; i.e. you may have a bunch of garbage chars at the end of your row, one of which might even be the character you’re looking for! The customary solution for this is to put a 0-valued character, written ‘\0’, at the end of each word, so the function knows to stop when it sees it. You’d need to add this to the end of each word you’re initializing, and you might need to bump MAX_CHARS up too to make room for it, because it is a character like everything else in your word.

    A far better solution is to turn your dict into a one-dimensional array of strings, i.e.

    char* dict[MAX_WORDS];
    

    but that requires you be comfortable with pointers and strings, so come back to it when you’re ready.

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

Sidebar

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.