Copy an array of char into a multidimensional array of char

I have a set of hard coded arrays of char and I want to use them in order to fill a multidimensional array of char.
Unfortunately I cannot use std::string.

I have written something like that:
1
2
3
4
5
6
7
8
9
10
11
12
class Node
{
  char name[10][64];

  Node()
  {
     sprintf_s(name[0][0], "name1");
     sprintf_s(name[1][0], "name2");
     sprintf_s(name[2][0], "name2");
     ......
  }
}


That solution doesn't work and I have an compile time error:
Couldn't match type 'char(&)[_Size]' against 'char'

but I don't understand the problem.

I also tried a different solution:
memcpy(&name[0][0], "name1", sizeof(name[10][60]))

but again this doesn't work.

What I'm doing wrong.
If it's possible I would avoid a solution with a cycle, is it possible?
Last edited on
name[0][0] isn't a string, it's a single character.

name[0] is the string you want to fill.


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
Last edited on
_s is microsoft trying to stick their oar in again. Its supposedly more secure than the normal one.
Ah. Aside from the portability issue, if Microsoft says their version of something is "more secure," then I immediately distrust it.

I have had enough problems with their computers that I have a deeply-rooted distrust for anything labeled with the Windows logo. And no, it's not just me.
The MS -s versions take an extra size parameter. However, if using C++ there are overloaded versions that automatically obtain the size by using the standard template technique to obtain array size.
The MS -s versions take an extra size parameter.

That sounds like snprintf().
Yes - except that for MS C++ the size parameter for sprintf_s can be omitted but can't be for snprintf(). There's also a difference re buffer overflow.

I prefer snprintf()
Last edited on
what is sprintf_s()?

Something from the C11 standard.
https://en.cppreference.com/w/c/io

MS may have introduced the concept, but the standard committee decided it was something worthy a decade ago. Better error handling.

This is a good example of how the C libraries in the C++ standard are deviating from the C standard.
Ah! Well, that explains why it wouldn't compile as C++...I think?
sprintf_s is part of Annex K, so you have to
#define __STDC_WANT_LIB_EXT1__ 1
before you
#include <stdio.h>
Then you can use it, if it is implemented (it's optional).

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf
Last edited on
Yes, this is an exercise and that's why I cannot use strings and I have to use arrays of char.

Then yes, I'm studing under Windows, using visual studio code with MS visual studio c++.

Thanks to all for the answers.
Topic archived. No new replies allowed.