String arrays

+4 votes
asked Apr 22, 2019 by anonymous
how to declare a string/char 2D array? Is it char result[100][100];?

how to insert hardcoded values in the 2D array? is it?

for (i = 0; i < p; ++i)


         if (strength[i] < energy[i])


            result[i][100] = "WIN";

            count = count + 1;

         } else

            result[i][100] = "LOSE";


how to print the data of the 2D array? is it?

for (i = 0; i < p; ++i)


         printf("%s\n", result[i][5]);



2 Answers

0 votes
answered May 25, 2019 by shubham sapkal (320 points)
In C++17 version, There is new feature ie that, it adds a new data type to us.

The new data type is the "string",

In older version, we declare string is " char [size] "

but which is use of " string " datatype , we can declare as " string string_name ".

so, its clear that to make 2D or multi-dimensional array.

Thanking you...
+1 vote
answered May 28, 2019 by Pierre (320 points)
An array of strings would be char *str[100][100], but that would only be an array of pointers. You would have to allocate memory (not recommended).

The STL way is:

vector<vector<std:string> > v2D(ROWs, vector<std:string>(COLs, initValue);  


Where 'ROWs' is the number of rows, and 'COLs' is the number of columns.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.