2

I'm first year student and I do C programming. Don't be so mean to me, please.

Could you show how to use and write multi-dimensional arrays in functions?

I have researched the arrays and function arg input.

I have read that the first dimension is not that important to the compiler. It checks the second and further dimensions in an array, so you have to specify it even in the function to make it work.

I tried different combinations for arrays to make it work but didn't find a solution.

There is a little piece of my code:

int size=5,location_x=10,location_y=10,s=NULL,l_x, l_y, status=2;

int stage_3(float[][int],int [],int [],int []);

int main()
{
float location[l_x][l_y];
int x[size], y[size], z[size];

if(!stage_3(location[l_x][l_y],x[size],y[size],z[size]))
    return 0;
}

int stage_3(float location[][int l_y],int x[size],int y[size],int wt[size])
{
  return 0;
}

13|error: expected expression before 'int'

13|error: expected ';', ',' or ')' before 'int'

I have idea that the problem are those [][]. They are not constants. The program makes them as variables that you could choose by scanf as you wish in accessible range for more flexibility.

5
  • You have very few options in c, you can use malloc() and dynamically allocate the arrays, or you con do something like int stage_3(size_t size, size_t l_y, float location[][l_y], int x[size], int y[size], int wt[size]) Commented Apr 17, 2015 at 15:25
  • @iharob So you want to say that if i do malloc(location[l_x][l_y]) it will initialize the memory block and you just simply use anywhere in any function? Commented Apr 17, 2015 at 15:32
  • No, that's incorrect and since you are a beginner you must avoid malloc() it's difficult to get to understand how exactly pointers work and also, with malloc() you must assume a new responsibility, to free() the malloc()ed blocks. Commented Apr 17, 2015 at 15:38
  • I'm sorry to be so dumb. But I didn't understand how works those memory blocks. I used size_t ones for strings, and there was pointer aswell. Do i need to use pointers for it? Omg, am I look that stupid... Commented Apr 17, 2015 at 15:38
  • 1
    No, you're just a Random Noob, and it's ok to be one, don't worry and avoid pointers until you feel you really understand all the other concepts. Commented Apr 17, 2015 at 15:39

1 Answer 1

3

This prototype cannot work:

int stage_3(float[][int],int [],int [],int []);

You need to give the actual size of the second dimension of the first array. If that size is not a compile-time constant, then your best option is probably to use a variable-length array. Here's a good way to do that:

int stage_3(int l_y, float location[][l_y],int x[],int y[],int z[]);

Of course, the function definition must be altered to match, and if you use an additional argument to express the variable dimension, as above, then you must include the extra argument in your function call as well.

VLAs were new in C99; some compilers still need to be instructed to use C99 mode to compile code that uses them.

Sign up to request clarification or add additional context in comments.

4 Comments

The extra argument (l_y) is already included (uninitialized) as a global, in other words: you don't need to include int l_y, just initialize it or assign some value at runtime. An example
Yes, you can rely on a global for the array dimension instead of including the dimension as a function argument. Doing so would allow a function signature more similar to the one the OP started with. As a matter of style and good practices, however, I do not recommend that approach.
I agree, is better to pass the dimension to the function, but you must include the extra argument in your function call is not true, because l_y is already defined as a global: int size=5,location_x=10,location_y=10,s=NULL,l_x, l_y, status=2;
Oh, I see what you're saying, @AlterMann. Ok, I have updated my answer to avoid implying that passing the size of the variable dimension as a function argument is the only way to use a VLA for this job. Since the OP is inexperienced, though, I'm going to spare him a detailed treatise on all the other alternatives he has, and stick to just recommending one.

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.