Public Group

Passing a 2D array of characters to a function

This topic is 3522 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Ok, I am having a serious brain lapse in how to do the following: I want the function printDatabase(int a[][NUM_GRADES], char names[][20]) to simply print out the information contained inside of a and names. However names is defined as the following
char studentNames[NUM_STUDENTS][20] = {{"Bob"}, {"John"}, {"Joe"}};


The function definition of printDatabase is the following:
// output data
void printDatabase( int a[][ NUM_GRADES ], const char names[][20] )
{
cout << "Here is the grade database\n\n"
<< setw( 10 ) << "Name";

for ( int n = 1; n <= NUM_GRADES; n++ )
cout << setw( 4 ) << n;

cout << endl;

for ( int i = 0; i < NUM_SUDENTS; i++ ) {
cout << setw( 10 ) << names[ i ];

for ( int j = 0; j < NUM_GRADES; j++ )
cout << setw( 4 ) << a[ i, j ];

cout << endl;

} // end for

cout << endl;

} // end printDatabase


The problem I am having is that I am throwing an error every time I try and do this and I can not remember how your suppose to pass the char array into the function. This is being done as exercises out of a book for my own personal enjoyment :). Thanks for any help.

Share on other sites
void foo( char** ppStrings );

or

void foo( char* pStrings[] );

Share on other sites
While it will teach you a tad about memory and pointers, this is retarded. Sounds alot like a school assignment (the retardedness was a dead give away).

try this:
std::vector<std::string> studentNames;studentNames.resize(3);studentNames[0] = "Bob";studentNames[1] = "John";studentNames[2] = "Joe";void Foo(std::vector<std::string> &students){ size_t sCnt = students.size(); for (size_t s = 0; s < sCnt; ++s) {  //print some shiz }}

or even:
struct Student{ Student(const std::string &name = "")  : Name(name) { } std::string Name; std::vector<int> TestScores;};std::vector<Student> students;students.resize(3);students[0] = Student("Bob");students[1] = Student("John");students[2] = Student("Joe");void Foo(std::vector<std::string> &students){ size_t sCnt = students.size(); for (size_t s = 0; s < sCnt; ++s) {  Student &stud = students[s];  cout << "Name: " << stud.Name << endl;  cout << "  Scores: ";  size_t tCnt = stud.TestScores.size();  for (size_t t = 0; t < tCnt; ++t)  {   cout << stud.TestScores[t] << " ";  }  cout << endl; }}

Share on other sites
Quote:
 Original post by toogreat4uchar studentNames[NUM_STUDENTS][20] = {{"Bob"}, {"John"}, {"Joe"}};

The initializer on the right does not fit the data type on the left.
{{"Bob"}, {"John"}, {"Joe"}} is a three dimensional array.
Maybe {"Bob", "John", "Joe"} works better?

But as already suggested, you should use the tools C++ priovides, namely strings and vectors.

Share on other sites
After re-looking over the code I corrected some things and sure enough the function that I thought was causing issues was not where I had the error. The char double subscripted array is perfectly valid way of passing the information to a function. I do appreciate the responses and I am aware of all the STL headers and how they work (mostly), however I am trying to revamp my knowledge of everything. Thanks!

Share on other sites
First off,

Quote:
 After re-looking over the code I corrected some things and sure enough the function that I thought was causing issues was not where I had the error.

You are aware, I hope, that error messages generally include a line number and a description of the problem? If they don't make sense, just fix the first one; sometimes a missing semicolon or whatever can cause a lot of confusion for the compiler, which doesn't read code in a very "human" way at all.

Quote:
 Original post by toogreat4uHowever names is defined as the following*** Source Snippet Removed ***

There we see most of your problems.

The first problem is you are trying to use arrays of characters to represent text. In C++, we represent text with instances of std::string. The second problem is that you're using an array with some "maximum size" to represent a set of things that clearly doesn't have a natural restriction on its size. In C++, we represent sequences of unknown length with a standard library container, such as std::vector. The third problem is that you are passing two parallel structures of data to represent the students. In C++, we create structured data to represent a single thing, and make a container of structures, instead of several parallel containers.

Thus, we would do something like

struct Student {  std::string name;  // The number of grades per student *does* appear to be a specific value  // for a good reason: that's how many subjects are being taught to the class.  // Of course, you might decide that this restriction is not a useful one,  // and use a container here as well.  int grades[NUM_GRADES];};void printDatabase(const std::vector<Student>& students) {  // ...}

Quote:
 I can not remember how your suppose to pass the char array into the function.

You are, in fact, doing it correctly. You have other errors (which you would have to fix even with modern C++ tools), as follows:

1) "NUM_STUDENTS" is mis-typed in one place.
2) To index into a multi-dimensional array, you must index each dimension separately: arr[j][k], not arr[j, k]. The latter means the same thing (in this case) as arr[k], which just gives you an array. (Passing an array to the operator<< overload for std::cout causes it to decay to a pointer, so you end up printing the pointer value.) If you don't understand why that is, read this.
3) (Possibly) Ensure that you are either 'using namespace std;', or explicitly qualify things from the std namespace with 'std::'. Also, note that 'setw' comes from <iomanip>, not <iostream>.

Quote:
 Original post by DevFredThe initializer on the right does not fit the data type on the left.

You are correct, but strangely, this doesn't seem to matter to the compiler. I assume that nested braces are more or less ignored for aggregate initializers.

Share on other sites
I think one of you problem resides here :
for ( int i = 0; i < NUM_SUDENTS; i++ ) {      cout << setw( 10 ) << names[ i ];<-- Declared as 2d array, but you are trying to access it as 1dfor ( int j = 0; j < NUM_GRADES; j++ )         cout << setw( 4 ) << a[ i, j ]; <-- might not do what you want it to do}

Share on other sites
The second one is a problem. The first is not, because the char[] decays to a char*, and passing a char* to std::cout::operator<< is a sensible thing to do (it interprets the pointed-at sequence of characters as a C string).

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 10
• 11
• 9
• 9
• Forum Statistics

• Total Topics
634148
• Total Posts
3015794
×