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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:15:05+00:00 2026-05-11T18:15:05+00:00

I am very new to C++ programming and you will see why. I want

  • 0

I am very new to C++ programming and you will see why.

I want to make a character array consisting of a few words that I want to search with a linear search function. Does this array have to be a two-dimensional array? For example:

char Colors[3][6] = {"red", "green", "blue"};

I tried it like this:

char Colors[] = {"red", "green", "blue"};

This gave me a “too many initializers” error.

I assume the 1st method is correct because it states the amount of elements in the array and the maximum length of an element, correct?

Now how would I implement a linear search function to find a word inside that array? Can I do something like the following:

(Assuming the linearSearch function has already been declared)

char searchKey;  
char element;

char Colors[3][6] = {"red", "green", "blue"};

printf("Enter the color to look for: \n");

scanf("%s", searchKey);

element = linearSearch(Colors, searchKey, ??); //?? is where I don't know what to enter

if (element != -1)  
{  
    printf("Found the word.\n");  
}  
else  
{  
    printf("Didn't find the word.\n");  
}

Is this possible? If so, what would the declaration look for the linearSearch function? I hope I provided enough information for this to be somewhat usable.

edit: Thanks all for the help, got the program working as intended.

  • 1 1 Answer
  • 1 View
  • 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-11T18:15:05+00:00Added an answer on May 11, 2026 at 6:15 pm

    You could make your linearSearch function to return the index of the search term in the array. Here is a sample program:

    #include <stdio.h>
    #include <string.h>
    
    int linearSearch (const char **Array, const char *searchKey, int arraySize) {
        for (int i = 0; i < arraySize; ++i) {
            if (strcmp(Array[i], searchKey) == 0)
                return i;
        }
    
        // We didn't find the searchKey in the Array
        return -1;
    }
    
    int main () {
        char *colors[] = { "red", "green", "blue" };
    
        int index = linearSearch (colors, "green", 3);
    
        if (index < 0) { // search string was not found
            printf("Search string not found!\n");
        }
        else {
            printf("String %s found at index %d\n", colors[index], index);
        }
    
        return 0;
    }
    

    We use the strcmp() function to compare the strings. It returns zero if strings match and nonzero if they don’t. To use it, you need to include the string.h header.

    But, as others have suggested, you should use STL if you can.

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

Sidebar

Related Questions

I'm very new to programming and I am trying to write a program that
I'm very new to programming, so be gentle. My goal is to make a
I am very new to programming and I can't seem to find a way
I'm still very new at programming, and our local SSIS genius isn't here today
I am very new to programming with jQuery and I am trying to retrieve
I am very new to programming in general, while reading about PHP I saw
I am very new to python programming and have yet to buy a textbook
I'm very much new to programming and have been doing fairly well so far.
I am very new at C programming and I am working on a firmware
I am very new to MPI programming, I am trying to run a program

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.