2D array using strings

I was stuck in some kind of homework task that was not rated (it was intended for practice).

I need 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 that is used to search for a name in a 2D array, the function should return 1 if else 0 is found.

When I call a function (which is empty now), I get this warning: passing argument 1 of 'find_name' from incompatible pointer type

Here are the important bits.

In the 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); 

Function

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

I am new to C (I'm a student) and I am completely confused about strings (string arrays, etc.).

+5
source share
3 answers

I assume you need a 2D array of char pointers. Your strNameList declaration is incorrect in both places of your program. You have:

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

But char[][N] declares a 2D array of chars , not char* . Therefore, you are warned by the compiler to which you assign a raft of pointer values ​​for elements of type char

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

 const char *strNameList[][2] 

which declares an array of unknown length of two char* arrays, which now matches your initialization lists. In addition, const added because (a) I assume that you do not plan to change this list of names in your function and (b) the string literal declarations assigned to char* through the initializer are undefined behavior in C and are officially deprecated in C ++, so you should not use it independently. Similarly, your search name probably also does not change, 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, if you do not have a crystal ball, your find_name() function is not able to find out with this information how many names are listed in the list of names. I would rather you see it now, and not be surprised at what happened next. you must either (a) end the list with the value of the token that find_name() knows find_name() , or (b) pass the number of names in the list to find_name() . To each his own, but I prefer the last of them:

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

and call it on the side of your caller:

 find_name(strNameList, sizeof(strNameList)/sizeof(strNameList[0]), strFindName) 
+9
source

Do it like this:

 #define STOPPER_NAMELIST NULL char * strNameList[][2] = { { "Luca","Daniel"}, {"Vivan","Desmond"}, {"Abdul","Justin"}, {"Nina","Marlene"}, {"Donny","Kathlene"} {STOPPER_NAMELIST, STOPPER_NAMELIST} }; size_t sizeNameList(const char * strNameList[][2]) { size_t size = 0; while ((strNameList[size][0] != STOPPER_NAMELIST) && (strNameList[size][0] != STOPPER_NAMELIST)) ++ size; return size; } int find_name(char * strNameList[][2], char strLookUp[]) { size_t size = sizeNameList(strNameList); ... } ... nSearch = find_name(strNameList, strFindName); 

This approach uses an open array ( [] ) of char * arrays with elements 2 .


Update:

You can add a stopper element to the array that manages the names, then there is no need to transfer the size of the array along with the array itself, since the size can be determined by scanning the elements of the array until a stopper is found.

+5
source

Your find_name() function find_name() for a two-dimensional array of characters, for example:

 char arr[][2] = { { 'a', 'b'}, ... 

if you want to make them the lines you need:

 char *arr[][2] = { {"John", "Smith"}, ... 

Then in the list of function parameters you need:

 void find_name(char *something[][2]) { printf("first name: %s, second name: %s\n", something[0][0], something[0][1]); 

And in your main() function, call it, simply:

 find_name(arr); 
+1
source

Source: https://habr.com/ru/post/1447588/


All Articles