2D array using strings

arrays, c, string

Solution

I'm assuming you want a 2D array of char pointers. Your declaration of strNameList is incorrect in both locations in your program. You have:

char strNameList[][2] = { { "Luca","Daniel"} ,{"Vivan","Desmond"},{"Abdul","Justin"}, {"Nina","Marlene"},{"Donny","Kathlene"} };

But `char[][N]` is declaring a 2D array of `chars`, not `char*` Therefore you're being warned by the compiler you're assigning a raft of pointer values to items of type `char`

Change both your declarations (your variable and your function parameter) to:

const char *strNameList[][2]

which declares an array of unknown length of arrays of two `char*`, which now matches your initialization lists. Also, the `const` is added because (a) I'm assuming you are not planning on modify that name list in your function, and (b) writable string literal declarations assigned to `char*` via initializer is undefined behavior in C, and officially deprecated in C++, so you should not be using it regardless. Likewise, your lookup-name is probably not being modified either, so also declare it `const`.

Result:

const char * strNameList[][2] = { 
    {"Luca","Daniel"} ,
    {"Vivan","Desmond"},
    {"Abdul","Justin"}, 
    {"Nina","Marlene"},
    {"Donny","Kathlene"} 
};

and in your function:

int find_name(const char * strNameList[][2], const char strLookUp[])

Last but certainly not least, unless you have a crystal ball your `find_name()` function has no way of knowing with the given information how many names are in the name list being passed. I'd rather you see this now rather than wonder what happened later. you need to either (a) terminate the list with a token-value that `find_name()` knows about, or (b) pass the number of names in the list to `find_name()`. To each their own, but I prefer the latter of these:

int find_name(const char * strNameList[][2], size_t nNameListSize, const char strLookUp[])

and invoke it on your caller side by:

find_name(strNameList, sizeof(strNameList)/sizeof(strNameList[0]), strFindName)

Problem

I'm stuck on some homework which isn't graded (its meant for practice). I have to create a function called `find_name` that takes 2 arguments. The first argument is a 2D array of names (strings), and the second is a character string which is used to find the name in the 2D array, the function must return 1 if found else 0. When i call the function (which is empty right now), I get this `warning: passing argument 1 of 'find_name' from incompatible pointer type` Here is the important bits. In Main ``` char strNameList[][2] = { { "Luca","Daniel"} ,{"Vivan","Desmond"},{"Abdul","Justin"}, {"Nina","Marlene"},{"Donny","Kathlene"} }; char strFindName[] = "\0"; printf("Please enter a name to look for: "); gets(strFindName); nSearch = find_name(strNameList, strFindName); ``` The Function ``` int find_name(char strNameList[][2], char strLookUp[]) ``` I'm new to C (I'm a student), and I'm completely confused about strings (string arrays etc).

Original source