First of all, what is
sprintf_s()? I have never seen that before, and it's not in any library that I have. When I wrote a short program to test the 2D array, I got errors telling me that "sprintf_s is an undeclared identifier."
If you're using a special non-standard library, you want to be careful because that can cause portability issues. Meaning your program will crash on someone else's computer. If it was just a mistake, then disregard my rant.
Moving on, your next problem is that you're trying to fill a single char variable
name[0][0] with an entire string. So, you're basically trying to put a whole word into a space that's only big enough to hold a letter. That's not going to work.
Here's a simple program that demonstrates a 2-dimensional C string like the one you're trying to implement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <cstdio> // needed for puts() and sprintf()
int main ()
{
char name[10][64];
// using name[0], which is a 1-dimensional array
// instead of name[0][0], which is a single char
sprintf (name[0], "name1");
sprintf (name[1], "name2");
sprintf (name[2], "name3");
// using puts() here because it's simpler than std::cout
puts (name[0]);
puts (name[1]);
puts (name[2]);
return 0;
}
|
$ c++ -std=c++2a -Wall -Wextra -pedantic untitled.cc -o a.out
$ ./a.out
name1
name2
name3
$ |
Why can't you use std::strings? They're much easier and more convenient to work with. Is this a homework assignment or something like that, that wants you to learn about C-strings?
But anyway, hope this helps!
max