2

Hi so Im fairly new to C.

Basically what I am asking is this:

So I have a 2-D char array, that is representing a list of strings (char array[size][20]).

I then have another string, which I am representing as a char pointed (char *word).

What I want my function to do, is to go through each of the strings in my array, and if it finds the word to return 1.

However Im quite confused about pointers, so I'm not sure how to write this. I started with just using a for loop, going through the lines of the array. And then if(word = array[i]) return 1. But this doesn't work for me.

I hope this made sense, if not I will try explain again. And I'll greatly appreciate any answers. I dont really need code. Just a step in the right direction.

ctype.h
  • 1,470
  • 4
  • 20
  • 34
Dylan
  • 21
  • 1

5 Answers5

5

Assuming that your array consists of null-terminated strings (i.e. strings of length at most 19), then strcmp is your solution:

#include <string.h>
#include <stddef.h>

size_t find_in_array(char array[][20], size_t size, char * word)
{
    for (size_t i = 0; i != size; ++i)
        if (strcmp(array[i], word) == 0)
            return i;

    return size;
}

This will return the offset if the word was found, and size otherwise.

Usage:

char array[3][20] = { "Hello", "World", "Jim" };

size_t i = find_in_array(array, 3, "Jim");  // returns "2"

The word was found if "i != 3".

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 1
    Any special reason for returning `size` and not `(size_t)-1`? –  Oct 30 '12 at 22:52
  • 1
    @H2CO3: Sure. "Not found" equals "end"-iterator :-) If C++ taught us one thing, it's how to [talk about ranges](http://stackoverflow.com/a/9963438/596781). – Kerrek SB Oct 30 '12 at 22:53
  • 1
    you're a C++ guy, right? :P (Also, I'd suggest changing `3` to `sizeof(array) / sizeof(*array)`.) And don't try to teach me about that :) In Cocoa, we also have `struct NSRange { unsigned location; unsigned length; }` –  Oct 30 '12 at 22:56
  • Thanks so much. Worked perfectly. Will read up about strcmp now to understand it. Thanks a million :D – Dylan Oct 30 '12 at 22:56
2

Your given comparison is only comparing the value of the pointer itself is equal, not that the contents of the memory that the pointers are pointing at are the same (also, in your example a single equal is assignment, not comparison).

Your array really looks like this diagram I scribbled up: Array of an array of characters

What you want to do is to use the strcmp or other such functions to compare that the memory pointed to by your word variable is equal to any of the individual strings in the array.

In languages with pointers, it helps to draw out memory like I have done here, and keep in mind that a pointer is not the actual contents of your memory but just a way to locate it.

dcgibbons
  • 376
  • 2
  • 3
  • 13
1

This is C, not C++. Using == will compare the strings' addresses numerically, and not their contents. To test for string equality, use the strcmp() function, which returns 0 if its arguments are the same string:

int has_word(const char *word, const char list[][20], size_t n)
{
    int i;
    for (i = 0; i < n; i++) {
        if (strcmp(word, list[i]) == 0) {
            return 1;
        }
    }
    return 0;
}

As per your needs, this function will return 1 if it finds the 0-terminated string word among the first n elements of the list array of strings.

  • 1
    Are we sure we're not confusing arrays, pointers, arrays of pointers and pointers to arrays? And that we're compiling with *all warnings*? :-) – Kerrek SB Oct 30 '12 at 22:58
  • 1
    @KerrekSB Haven't compiled this using a compiler, only in brain :P Actually, I used `index` for the sake of verbosity, and only completed it to an entire function, but there I used the idiomatic `index`... Also, undeclared identifiers don't need `-Wall`, they throw an error immediately (if the creator of a compiler cares about his reputation...) –  Oct 30 '12 at 23:01
  • 1
    @KerrekSB and where did I make the pointer vs. array confusion? –  Oct 30 '12 at 23:02
  • 1
    Well, the array is profoundly wrong, I'm afraid. Everything else is a minor ailment. But please do try to compile this. – Kerrek SB Oct 30 '12 at 23:02
0

Take a look at the functions in string.h, specifically strstr().

Wikipedia has a pretty decent page on C String Handling.

tomlogic
  • 11,489
  • 3
  • 33
  • 59
0

At first,word = array[i] is definitely not a good way to compare strings. You should use strcmp: http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

I have just answered another question really similar to yours. Look at this question, it could give you some ideas: Returning and printing string array index in C

Community
  • 1
  • 1
Joze
  • 1,285
  • 4
  • 19
  • 33