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)
Answer from WhozCraig on Stack Overflow
Top answer
1 of 3
12

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)
2 of 3
5

Do it this way:

#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 2 entries.


Update:

You could add a stopper element to the array carring the names, then there is no need to pass around the array's size along with array itself, as the size could alway be determined by scanning the array members until the stopper is found.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ array-of-strings-in-c
Array of Strings in C - GeeksforGeeks
July 23, 2025 - In C, an array of strings is a 2D array where each row contains a sequence of characters terminated by a '\0' NULL character (strings).
๐ŸŒ
Medium
allwin-raju.medium.com โ€บ print-2d-array-of-strings-in-c-dc6594c8d9f1
Print 2d array of strings in C. This article focuses on printing theโ€ฆ | by Allwin Raju | Medium
December 15, 2020 - But there is a method to print ... called puts(). ... The C library function int puts(const char *str) writes a string to stdout up to but not including the null character....
๐ŸŒ
Raspberry Pi Forums
forums.raspberrypi.com โ€บ board index โ€บ programming โ€บ c/c++
Two-dimensional array of character strings in C - Raspberry Pi Forums
You can choose between making arrays ... array as 2d, like this ... printf("%s", data[i + (j * max_i)]); But this would still need to be an array of pointers to char. There are many ways to do things in C, it would help to understand exactly what you are trying to do.
๐ŸŒ
Know Program
knowprogram.com โ€บ home โ€บ two dimensional (2d) array of strings in c
Two Dimensional (2D) Array of Strings in C - Know Program
October 9, 2021 - 2d array of strings in C. char student[5][20]; First index (row-size) specify number of strings needed & 2nd index (column-size) specifies length of string.
๐ŸŒ
Quora
quora.com โ€บ In-C-is-an-array-of-strings-the-same-thing-as-a-two-dimensional-array-Considering-that-strings-are-arrays-of-characters
In C, is an array of strings the same thing as a two dimensional array? Considering that strings are arrays of characters. - Quora
Answer (1 of 6): Oh, nice: another can of wormsโ€ฆ There are two types of โ€œmultidimensionalโ€ arrays in C: square arrays and ragged arrays. The key to understanding is defining what an element of the array is.
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 98206-how-pass-2-dimensional-array-strings-function.html
How To pass 2 dimensional array of strings to a function
void func(char** names, n) { for (int i=0; i<n; i++) puts(names[i]); } // then call like this: func(temp, n); A double pointer won't work as a replacement for a 2D array - it only works the other way around, that you can make a 2D array by using a double pointer.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2218440 โ€บ how-to-find-a-string-in-a-2d-array-of-char-in-c
How to find a string in a 2D array of char in C | Sololearn: Learn to code for FREE!
That basically compares their addresses since a string is a char array. In <string.h>, there is strcmp(char*, char*), Which compares 2 strings and return 0 if they are equivalent. In a nutshell, include <string.h> and use !strcmp(arr[i], ch). ... the language in Arabic is wrong the English words with the Arabic words make faults please fix this problem fastest you can
Find elsewhere
๐ŸŒ
SkillVertex
skillvertex.com โ€บ blog โ€บ array-of-strings-in-c
Array Of Strings In C -
May 10, 2024 - An array of strings is essentially a 2D array of characters, where each row (or element in the first dimension) represents a string (a sequence of characters terminated by a null character โ€˜\0โ€™).
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ cprogramming โ€บ c array of strings
C Array of Strings
June 10, 2012 - Assuming that the array is located at the memory address 1000, the logical layout of this array can be shown as in the following figure โˆ’ ยท To use the memory more efficiently, we can use the pointers. Instead of a 2D char array, we declare a 1D array of "char *" type.
Top answer
1 of 3
2

I know at all arrays in C are essentially pointer to the first element of an array

Not quite. Arrays and pointers are two different things entirely. Except when it is the operand of the sizeof, _Alignof, or unary & operator, or is a string literal being used to initialize an array in a declaration, an expression of type "N-element array of T" will be converted to an expression of type "pointer to T" and its value will be the address of the first element in the array.

Given the declaration

int a[10];

the object that a designates is always and forever a 10-element array of int; however, the expression a may be treated as a pointer to the first element.

If you know your strings will never be more than 19 characters long (20 elements including the terminator), but don't know the number of strings ahead of time, you can do something like this:

char (*mens_names)[20];
char (*womens_names)[20];
...
mens_names = malloc(number_of_couples * sizeof *mens_names);
womens_names = malloc(number_of_couples * sizeof *womens_names);
...
fscanf(input_file, "%s", mens_names[i]);
...
free(mens_names);
free(womens_names);

In this case, we've declared mens_names and womens_names as pointers to 20-element arrays of char (the parentheses matter). Thus, sizeof *mens_names is equivalent to sizeof (char [20]).

You would access each individual character as you would with a regular 2-d array:

char x = mens_names[i][j];

mens_names[i] implicitly dereferences the mens_names pointer (remember that the expression a[i] is interpreted as *(a + i)).

This method has a couple of advantages over KBart's method. First, all the memory is allocated contiguously as a single chunk, which may matter if caching becomes an issue. Secondly, you only need one malloc and one free for each array. Of course, this assumes that the maximum size of each name array is a) fixed and b) known at compile time.

If you won't know the size of the name until runtime, and you're using a C99 compiler or a C2011 compiler that supports variable-length arrays, you can do something like this:

size_t name_len, number_of_couples;
// get name_len from the user or input file
// get number_of_couples
char (*mens_names)[name_len+1] = malloc(number_of_couples * sizeof *mens_names);
...

If you won't know the size of the name until runtime, and you're using a compiler that doesn't support VLAs, then you'll need to use KBart's method.

If you wanted to get really fancy, you could use a single 3-dimensional array instead of two 2-dimensional arrays:

#define MENS_NAMES 0
#define WOMENS_NAMES 1
...
char (*all_names)[2][20] = malloc(number_of_couples * sizeof *all_names);
...
fscanf(input_file, "%s", all_names[i][MENS_NAMES]);
...
free(all_names);
2 of 3
2

You are talking about 2D array, but initializing it only as a one dimensional array. The correct initialization of 2D array (matrix) is as follows:

static char** allocate_matrix(int nrows, int ncols) 
{
    int i;
    char **matrix;

    /*  allocate array of pointers  */
    matrix = malloc( nrows*sizeof(char*));

    if(matrix==NULL)
        return NULL; /* Allocation failed */

    /*  Allocate column for each name  */
    for(i = 0; i < nrows; i++)
        matrix[i] = malloc( ncols*sizeof(char));

    if(matrix[i-1] == NULL) 
        return NULL; /* Allocation failed */

    return matrix;
}

In your main():

<...>
mens_names = allocate_matrix(number_of_couples, 19);
womens_names = allocate_matrix(number_of_couples, 19);
<...>

/* Of course, do not forget to free memory once you are done */
๐ŸŒ
Sdds
intro2c.sdds.ca โ€บ two-dimensional arrays
Two-Dimensional Arrays | Introduction to C - Table of contents
A simple data structure for organizing tabular data is the two-dimensional array. The C language supports multi-dimensional arrays. The C compiler treats a two-dimensional array as an array of arrays. An obvious application of this data structure is an array of character strings.
๐ŸŒ
CodinGeek
codingeek.com โ€บ home โ€บ 2d character array-string array-declaration and initialization
How to Initialize & Declare 2D character array in C?
April 16, 2021 - A 2D character array is more like a String array. It allows us to store multiple strings under the same name. A 2D character array is declared in the following manner: ... The order of the subscripts is important during declaration.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ what is an array of strings in c?
What is an Array of Strings in C? - Scaler Topics
April 30, 2024 - As we know, strings are nothing but an array of characters; hence, the array of a string would be a two-dimensional array of characters. We can use a 2D character array to declare an array of strings in C.
Top answer
1 of 3
1

If you actually know the size of the array before running the program, you don't need to dinamically allocate the memory with malloc, you could create a 2D static array. In your case, as it is a 2D array of strings, it could be declared as char * array[ROWS][COLS], and then you could asign a string to a specific element this way: array[nrow][ncol]="Your String".

2 of 3
1

C, unlike Java, actually has a concept of multidimensional arrays; so unless there's a specific reason you want a char * * *, you might prefer to write:

char (*people)[COLS][NAME] = malloc(ROWS * sizeof(*people));

which sets people to be a pointer to the first of ROWS dynamically-allocated two-dimensional character arrays.

Due to pointer "decay", where an expression of array type will double as a pointer to the first element of the array, you can use people very much as if it were a char * * *; for example, people[3][4] will point to the string in row 3, column 4. The only restriction is that you can't write something like people[3][4] = ... to suddenly change what string to point to. But it sounds like you don't want to do that, anyway?


Note: the above is assuming that you are intentionally using dynamic memory allocation. However, I do recommend you consider Sizigia's suggestion to use static memory, which is the same sort of storage as is used for global variables. If you write something like

static char people[ROWS][COLS][NAME];

then the memory will be allocated just once, at the start of the program, and reused by all calls to the function that declares it.