4

How to assign string value in multidimensional array, send the array as a function argument, and return it back to main function? I tried this but it gives an error:

char a[250][250][250];   // does not work
a[][0][2] = "0";         // does not work
a[][1][2] = "0";         // does not work

char a[][2][2] = {"0", "1"};  // works

// error: expected primary-expression before ']' token
a[i][j][max] = add_func(a[i][j][], i, j); 
3
  • You're confusing characters and strings. Your array is array of chars, not strings. Commented Dec 30, 2012 at 8:59
  • 5
    15.6MB of string data. poor, poor stack. Commented Dec 30, 2012 at 9:01
  • 1
    char *a[250][250] will save 96% space. Commented Dec 30, 2012 at 9:38

3 Answers 3

7

To initialize character strings table you can use curly braces. And the outer most dimension (I dont know how else can it be called) or the left most number in square is optional.

So this will work

char table[][3][10] = {
    {"row1-col1", "row1-col2", "row1-col3"},
    {"row2-col1", "row2-col2", "row2-col3"},
    {"row3-col1", "row3-col2", "row3-col3"},
    {"row4-col1", "row4-col2", "row4-col3"}
    };

You dont need to type table[4][3][10]. Compiler calculates it. The size of table is 120 bytes. As the contents are all string you can use

char *table[][3] = ...

This will save 20% space.

Curly braces can only be used in initializing phase. Not after that. hence following code will not work.

a[][0][2] = "0"; 
Sign up to request clarification or add additional context in comments.

1 Comment

+1, and a mentor long ago called it the "superior" dimension of the array. All others he referred to as "inferior" dimensions. I've also seen them called "dominant" and "sub-dominant".
6

After declaration you can not assign, but use strcpy()

char a[250][250][250];

strcpy(a[0][1],"0");

or assign at the time of declaration:

char a[250][250][250] = {"0","2"};  
char a[][250][250] = {"0","2"}; 

or if you want to assign a single char.

a[i][j][k] = '0'; 

Where i, j, k are any value less than 250


How to Declaration and Initialization 3D Array IN C

In-general a[3][4][2] is a three-dimension array, that can be view as

a[3][4][2] : Consist of 3 two-dimension arrays, where each 2-D array are consist of 4-rows and 2-colunms. can be declare as:

char a[3][4][2] =  { 
                       { //oth 2-D array 
                         {"0"},
                         {"1"},
                         {"2"},
                         {"4"}
                       },
                       { //1th 2-D Array
                         {"0"},
                         {"1"},
                         {"2"},
                         {"4"}
                       },
                       { //2nd 2-D array
                         {"0"},
                         {"1"},
                         {"2"},
                         {"4"}
                       },
                   };  

Note: "1" means two chars, one additional fro null ('\0') char.

If integer array:

int a[3][2][3]=  
        {
            { //oth 2-D array, consists of 2-rows and 3-cols
            {11, 12, 13},
            {17, 18, 19}
            },
            {//1th 2-D array, consists of 2-rows and 3-cols
            {21, 22, 23},
            {27, 28, 29}
            },
            {//2th 2-D array, consists of 2-rows and 3-cols
            {31, 32, 33},
            {37, 38, 39}
            },
        };

Link to understand


Second error:

to this a[i][j][max] a char can assign not string so,

a[i][j][max] = '0' ; // is correct  expression 

but

a[i][j][max] = "0";  // is not correct, wrong   

Please read WhozCraig comment. you are declaring huge memory in stack!


According to your comment :

function declaration:

char add_func(char a[250][250][250], int i, int j); // function definition  

try like this:

  char a[250][250][250];
  a[i][j][max] = add_func(a, i, j );

7 Comments

@sabbir_pstu : Which passing to a function ?
the present value of a[][][] will be passed to function as function parameter and return other multidimensional array variable.
@sabbir_pstu ok I guess it should be like add_func( a, i, j ); can't assign to a[i][j][max]
@sabbir_pstu: you need to show definition of add_func, then I can help you.
char add_func(char a[250][250][250], int i, int j); // function defination
|
2

You probably would like to use pointers instead:

char *a[2][2] = { "0", "1", "2", "3" };

2 Comments

Assuming each entry isn't going to be altered, that will work.
Definitely true. But you still can assign individual strings by constructs like a[i][j] = "something" or a[i][j] = strdup(something).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.