When I am trying to pass two dimensional array as a parameter In C,
void PrintTriangle(int *triangle)
It's doesn't works for me why ?
You must specify the dimension of the outermost array when passing multidimensional arrays as parameters
Something like this:-
void PrintTriangle(int pascalTriangle[][5]);
Calling PrintTriangle(pascalTriangle); like this is wrong as your function PrintTriangle() is expecting a pointer to integer.
Here's how I memorized passing multidemensional arrays as parameters. This might be a little childish but works for me.
Suppose You have an array defined
int numbers[3][4];
and you want to pass it to function.
To do it correctly I answer series of questions:
1) What do I want to pass to function? An array.(in this case numbers).
2) What's numbers? Numbers is an address of its first element(without going into details);
3)What's the first element of numbers?(in this case it's an array of 4 integers)
4) So what I want as function formal parameter is a pointer to answer from step 3:
int (*pointer)[4]
It might be slow and looks unprofessional but you can ask the same question on array of every dimension you can think of. It's also useful to ask yourself that when not passing whole array but one of subarrays.
Hope it helps you as it helped me.
Calling your function as
PrintTriangle(pascalTriangle);
is wrong. Because PrintTriangle() expecting a pointer to integer as its argument and pascalTriangle (after decaying) is of type int(*)[5].Also you must have to pass the number of rows and columns (as arguments) in array pascalTriangle. Prototype for this function should be
void PrintTriangle(int row, int col, int *triangle);
and a call to it may be
PrintTriangle(5, 5, &pascalTriangle[0][0]);
Related
Is it possible to pass a 2-D array with variable row and column to a function and modify it?
Based on the resources on line I got, How to pass a 2D array as a parameter in C and Passing 2-D Array to a Function in C, seemingly I need a fixed row and column array.
Thanks,
I just got a possible solution for the question.
Based on "passing 2d array as parameter to function in c” Code Answer", the function is defined as void fun(int *arr, int m, int n).
But if there are any other sophisticated answers, please share with me.
Thanks,
I recently saw a function written in the following form void func(int size, int a[size]){ ... } and I was curious about the argument a[size]. At first I thought maybe it had something to do with indexing the array, but running the code a was in fact an array. Then I thought maybe it had to do with the size of the array, so I tried passing in different value and seeing if it would affect the array length, but it seemed not to (unless maybe it had something to do with writing past the array, but this feels unlikely). So my question is essentially, what does the "index" do in the function arguments?
It's the array size, but it's ignored when it's in a function parameter declaration. A function parameter of the form int a[size] or int a[] is treated identically to int *a, because arrays decay to pointers when used as function arguments.
So it's essentially just a form of self-documentation. The caller is supposed to pass an array that contains size elements.
Note that this only applies to the first dimension in a multidimensional array parameter. If you have something like
void func(int width, int height, int a[height][width]) { ... }
The width part of the array declaration is used, but the height is ignored. a is a pointer to an array of int[width] rows; the width is needed when indexing the first dimension.
I got a task to modify the content of a 2-dimensional array int[5][5], I was given the definition int *a[5][5] and ordered to use a int** (the pointer of a pointer) to handle this task.
I'm now wondering the meaning of this int *a[5][5], how can I understand the meaning of this and similar definitions?
int *a[5][5] is a 2D array of pointers. A pointer-to-pointer can be used to point at any pointer item in this array.
A for how to understand the declaration, everything left of the variable name is the type of each item in the array, in this case int*.
You could also use this site. It works for many C declarations, but not all.
It is nothing but a Matrix of Pointers
In fact there are meny questions on stackoverflow on these. Please refer cdecl.org
C newbie here, I need some help: Can anyone explain to (and offer a workaroud) me why this works:
int n=1024;
int32_t data[n];
void synthesize_signal(int32_t *data) {
...//do something with data}
which let me alter data in the function; but this does not?
int n=1024;
int number=1024*16;
int32_t data[n][2][number];
void synthesize_signal(int32_t *data) {
...//do something with data}
The compiler error message is something like it expected int32_t * but got int32_t (*)[2][(sizetype)(number)] instead.
First, passing arrays in C is by reference. So you pass a pointer of some sort, and the function can modify the data in the array. You don't have to worry about passing a pointer to the array. In fact, in C there is no real different between a pointer that happens to be to the being of an array, and the array itself.
In your first version. You making a one-dimensional array data[n], and you are passing it to your function. In the array, you'll using it by saying, something like data[i]. This translates directly to (data + (i sizeof(int32_t)). It is using the size of the elements in the array to find the memory location that is i positions in front of the beginning of your array.
int n=1024;
int number=1024*16;
int32_t data[n][2][number];
void synthesize_signal(int32_t *data)
In the second case, you're setting up a mufti-dimensional array (3D in your case). You setup correctly. The problem is that when you pass it to the function, the only thing that gets passed the address of the being of the array. When it gets used inside the function, you'll do something like
data[i][1][x] = 5;
Internally C is calculating how from the beginning of the array this location is. In order for it to do that, it need to know the dimensions of the array. (Unlike some newer languages, C store any extra data about array lengths or sizes or anything). You just need to change the function signature so it knows the shape/size of array to expect. Because of the way, it calculates array positions, it doesn't need the first dimension.
In this case, change your function signature to look like this:
void synthesize_signal(int32_t data[][2][number]) { ...
Setup the array the same way you are doing the second one above, and just call it you'd expect:
synthesize_signal(data);
This should fix everything for you.
The comments mention some useful information about using more descriptive variable names, and global vs. local variable. All valid comments to keep in mind. I just addressed to code problem you're having in terms of mufti-dimensional arrays.
try
synthesize_signal(int32_t** data)
{
}
Your function also needs to know that data is multi dimensional. You should also consider renaming your data array. I suspect that it is a global variable and using the same name in function can lead to problems.
When you call the function, do it like this:
synthesize_signal(&data[0][0][0]);
I thought of doing
int arr[row][col];
But I guess since I am to pass the entire arr to a function multiple number of times, hence it may give me stackoverflow [since ROW and COL can be a few thousands]. Hence if I do it using pointers instead then passing on the pointer would be a better way , since I also intend to change the values of the array as it passes through various functions.
How do I define the array using pointer and how do I pass it to the function? Intend to do arr[i][j] whenever I want to access an element.
When you pass arrays around as arguments, you only pass a pointer to its first element, not the entire array.
So the function signature could look something like this:
void some_function(int arr[][col]);
Or optionally
void some_function(int (*arr)[col]);
If the column size is not a global compile-time constant, then you can pass it as argument to the function as well:
void some_function(const size_t col, int arr[][col]);
Or
void some_function(const size_t col, int (*arr)[col]);
Passing the array to a function is OK. Arrays are treated like pointers when passing as argument, that is, only the address of the first element is copied.
But be sure to pass number of rows and columns as arguments too.