I'm in a CS class in college now, and it's in C++. I have done all the things we're covering this semester... but in Java.

I'm trying to write a basic program that uses a 3d char array to store the last names of passengers in an airplane, but when I try to initialize the array, i'm getting compiler issues. I have no idea what is wrong, and it's bugging the hell out of me. I guess I'm not used to using character arrays (we're not allowed to use strings for some reason).

Anyways, here's the part that's messed up:

char list[15][6][20]; //this should create an array with 15 rows/6 columns that holds char lists of 20 length, right?

for (int i = 0; i < 15; i++)
{
     for (int j = 0; j < 6; j++)
     {
            list[i][j] = "*"; //I also tried using "*\0" to no avail.
      }
}

Further, we're supposed to make our own functions to copy and compare character arrays. Any tips?

Thanks

Recommended Answers

All 4 Replies

what i dont understand is, y do you need a 3d array to hold only the last names of the passengers? just a 2d array shd work well.. and this will not create an array with 15 rows and 6 columns.. it will be 15 2d arrays with each 2d array is actually a collection of 6 1D arrays capable of holding 20 chars..

Yea, it will create 15 2d arrays... but that's kind of what I want. 15 rows, 6 columns, each column can hold an array of characters. It will allow me to reference by 2 dimensions rather than one.

ok..then while fetching you need another loop... to take the 3rd subscript .. it should b something like

list[j][k]

list[i][j] = "*"; //I also tried using "*\0" to no avail.

Any tips?

When you are using char-arrays, you can't just asign a number of chars to it like you did with strings. Example: char test[20] = "This works";

char test[20]; 
test[20] = "This doesn't work";

The second one expects only 1 char, so test[20] = '*' would work. (notice the single quotes ' )

If you've already declared an array of chars of a specific size (wether it is multidimensional or not) you should use strcpy() to get a value in it:

int main()
{
    char test[] = "Empty";
    char list[15][6][20];
    for (int i = 0; i < 15; i++)
    {
         for (int j = 0; j < 6; j++)
         {
               strcpy(list[i][j], test);
          }
      }
return 0;
}

This will fill your 15x6 char arrays with the word 'empty'


Niek

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.