简体   繁体   中英

Searching for an element in 2D array, C programming

I'm a noob at C programming and I'm having some difficulties making a string list and searching for a specific element.

#include <stdio.h>
#include <string.h>

# define MAX 6
int main(){
    char word[MAX];
    char x[MAX][20];
    int i;

    strcpy(x[0], "One");
    strcpy(x[1], "Two");
    strcpy(x[2], "Three");
    strcpy(x[3], "Four");
    strcpy(x[4], "Five");
    strcpy(x[5], "Six");
    printf("%s", "Search:");
    scanf("%s", word);

    for (i=0; i<6; i++) {
        if (x[i] == word) {
            printf("%s", "Found a match!");
        }
    }

    return 0;
}

It's never executing the statement present in the if block (ie, printf("Found a match!")) . Any idea why it is not executing the above mentioned statement? Thanks!

Use

if(strcmp(x[i],word) == 0)
printf("Found match\n");

== can't be used to compare strings as you are doing it.

This only compares the pointers and not the strings

It never returns "Found a match!". Any idea why?

Reason:
In C, array names are converted to pointers to their first elements ( with some exceptions there). x[i] == word is comparing two pointers instead of comparing strings . Since the base addresses of both arrays are different, comparison returns a false value.

Correction:

Use strcmp to compare two strings.

This

if (x[i] == word)

should be

if (strcmp(x[i], word) == 0)

In ca predefined function is present in string.h library it is strcmp as stated by other users function int strcmp(const char *str1, const char *str2) compares the string pointed to bystr1 to the string pointed to by str2.u can write your own function for comparing strings and use it. I want you to conceptually understand why we can't use == in c unlike c++ as c don't contain anything like string class(c is purely procedural) so that u can create object of it and use it.hence c uses char array to represent a string .if u examine ur code x[i] == word compares starting addresses of char arrays/strings x[i],word. I believe u understood the concept . now I want to explain that u can use pointers here ie

if (*x[i] == *word) 
    printf("Found a match!");                 

Works fine as u can understand that here we are comparing two strings directly by pointing to their address locations.sorry if I have provided unwanted info due to my inexperience in SO as this my first answer in SO.

use strcmp() function for comparing two string. when two string is match its result is 0 . so you can change like :

if ( ! strcmp(word,x[i]) )  // when match result will be `! 0 = 1`
    printf("Found Match\n");

in the C programming language, the == operator is not working for comparing strings(as others wrote before me). I advice to try using a really nice feature in C++ called string. It is builded in the standard library, and with this, you can use the == operator for comparing.

#include <iostream>
using namespace std;

int main(void)
{
    string a = "Apple";
    if( a == "Apple" ) cout << "This is working" << endl;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM