Returning a two-dimensional array in C? - c

I recently started programming C just for fun. I'm a very skilled programmer in C# .NET and Java within the desktop realm, but this is turning out to be a bit too much of a challenge for me.
I am trying to do something as "simple" as returning a two-dimensional array from a function. I've tried researching on the web for this, but it was hard for me to find something that worked.
Here's what I have so far. It doesn't quite return the array, it just populates one. But even that won't compile (I am sure the reasons must be obvious to you, if you're a skilled C programmer).
void new_array (int x[n][n]) {
int i,o;
for (i=0; i<n; i++) {
for (o=0; o<n; o++) {
x[i][o]=(rand() % n)-n/2;
}
}
return x;
}
And usage:
int x[n][n];
new_array(x);
What am I doing wrong? It should be mentioned that n is a constant that has the value 3.
Edit: Here's a compiler error when trying to define the constant: http://i.imgur.com/sa4JkXs.png

C does not treat arrays like most languages; you'll need to understand the following concepts if you want to work with arrays in C.
Except when it is the operand of the sizeof or unary & operator, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element of the array. This result is not an lvalue; it cannot be the target of an assignment, nor can it be an operand to the ++ or -- operators.
This is why you can't define a function to return an array type; the array expression will be converted to a pointer type as part of the return statement, and besides, there's no way to assign the result to another array expression anyway.
Believe it or not, there's a solid technical reason for this; when he was initially developing C, Dennis Ritchie borrowed a lot of concepts from the B programming language. B was a "typeless" language; everything was stored as an unsigned word, or "cell". Memory was seen as a linear array of "cells". When you declared an array as
auto arr[N];
B would set aside N "cells" for the array contents, along with an additional cell bound to arr to store the offset to the first element (basically a pointer, but without any type semantics). Array accesses were defined as *(arr+i); you offset i cells from the address stored in a and dereferenced the result. This worked great for C, until Ritchie started adding struct types to the language. He wanted the contents of the struct to not only describe the data in abstract terms, but to physically represent the bits. The example he used was something like
struct {
int node;
char name[14];
};
He wanted to set aside 2 bytes for the node, immediately followed by 14 bytes for the name element. And he wanted an array of such structures to be laid out such that you had 2 bytes followed by 14 bytes followed by 2 bytes followed by 14 bytes, etc. He couldn't figure out a good way to deal with the array pointer, so he got rid of it entirely. Rather than setting aside storage for the pointer, C simply calculates it from the array expression itself. This is why you can't assign anything to an array expression; there's nothing to assign the value to.
So, how do you return a 2D array from a function?
You don't. You can return a pointer to a 2D array, such as:
T (*func1(int rows))[N]
{
T (*ap)[N] = malloc( sizeof *ap * rows );
return ap;
}
The downside to this approach is that N must be known at compile time.
If you're using a C99 compiler or a C2011 compiler that supports variable-length arrays, you could do something like the following:
void func2( size_t rows, size_t cols, int (**app)[cols] )
{
*app = malloc( sizeof **app * rows );
(*app)[i][j] = ...; // the parens are necessary
...
}
If you don't have variable-length arrays available, then at least the column dimension must be a compile-time constant:
#define COLS ...
...
void func3( size_t rows, int (**app)[COLS] )
{
*app = malloc( sizeof **app * rows );
(*app)[i][j] = ...;
}
You can allocate memory piecemeal into something that acts like a 2D array, but the rows won't necessarily be contiguous:
int **func4( size_t rows, size_t cols )
{
int **p = malloc( sizeof *p * rows );
if ( p )
{
for ( size_t i = 0; i < rows; i++ )
{
p[i] = malloc( sizeof *p[i] * cols );
}
}
return p;
}
p is not an array; it points to a series of pointers to int. For all practical purposes, you can use this as though it were a 2D array:
int **arr = foo( rows, cols );
...
arr[i][j] = ...;
printf( "value = %d\n", arr[k][l] );
Note that C doesn't have any garbage collection; you're responsible for cleaning up your own messes. In the first three cases, it's simple:
int (*arr1)[N] = func(rows);
// use arr[i][j];
...
free( arr1 );
int (*arr2)[cols];
func2( rows, cols, &arr2 );
...
free( arr2 );
int (*arr3)[N];
func3( rows, &arr3 );
...
free( arr3 );
In the last case, since you did a two-step allocation, you need to do a two-step deallocation:
int **arr4 = func4( rows, cols );
...
for (i = 0; i < rows; i++ )
free( arr4[i] )
free( arr4)

Your function return void, so the return x; line is superfluous. Aside from that, your code looks fine. That is, assuming you have #define n 3 someplace and not something like const int n = 3;.

You can't return an array in C, multidimensional or otherwise.
The main reason for this is that the language says you can't. Another reason would be that generally local arrays are allocated on the stack, and consequently deallocated when the function returns, so it wouldn't make sense to return them.
Passing a pointer to the array in and modifying it is generally the way to go.

To return (a pointer to) a newly-created array of dimensions known at compile time, you can do this:
#define n 10 // Or other size.
int (*new_array(void))[n]
{
int (*x)[n] = malloc(n * sizeof *x);
if (!result)
HandleErrorHere;
for (int i = 0; i < n; ++i)
for (int o = 0; i < n; ++o)
x[i][o] = InitialValues;
return x;
}
…
// In the calling function:
int (*x)[n] = new_array();
…
// When done with the array:
free(x);
If the size is not known at compile time, you cannot even return a pointer to an array. C does support variable-length arrays but not in the return types of functions. You could instead return a pointer to a variable-length array through a parameter. That requires using a parameter that is a pointer to a pointer to an array of variable length, so it gets somewhat messy.
Also, the preferred choices between allocating an array in the caller dynamically, allocating an array in the caller automatically, allocating an array in the called function dynamically and using variable-lengths arrays or fixed-length arrays or even one-dimensional arrays with manual indexing depend on context, including what how large the array might be, how long it will live, and what operations you intend to use it for. So you would need to provide additional guidance before a specific recommendation could be made.

In C there's only pass/return by value (no pass by reference). Thus the only way of passing the array (by value) is to pass its address to the function, so that it can manipulate it through a pointer.
However, returning by value an array's address isn't possible, since by the time control reaches the caller, the function goes out of scope and its automatic variables go down with it too. Hence if you really have to, you can dynamically allocate the array, populate and return it, but the preferred method is passing the array and leaving the onus of maintaining the array to the caller.
As for the error, the only warning I get in GCC for this is warning: 'return' with a value, in function returning void which is simply meaning that you shouldn't return anything from a void function.
void new_array (int x[n][n]); what you're really doing here is taking a pointer to an array of n integers; the decayed type is int (*x)[n]. This happens because arrays decay into pointers generally. If you know n at compile time, perhaps the best way to pass is:
#define n 3
void new_array (int (*x)[n][n]) {
int i,o;
for (i=0; i<n; i++) {
for (o=0; o<n; o++) {
x[i][o]=(rand() % n)-n/2;
}
}
}
And call it as
int arr[n][n];
new_array(&arr);

You can pass around arbitrarily dimensions arrays like any another variable if you wrap them up in a struct:
#include <stdio.h>
#define n 3
struct S {
int a[n][n];
};
static struct S make_s(void)
{
struct S s;
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
s.a[i][j] = i + j;
}
return s;
}
static void print_s(struct S s)
{
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
printf(" %d", s.a[i][j]);
printf("\n");
}
}
int main(void) {
struct S s;
s = make_s();
print_s(s);
return 0;
}

You are probably declaring n as a constant integer:
const int n = 3;
Instead, you should define n as a preprocessor definition:
#define n 3

Related

Pass pointer of an array of multi-dimensional arrays to a function, specifying bounds

Let's say I have multiple (a variable number) of 2D arrays (which may even be variable-length):
int a[2][2] = {{1,2},{3,4}};
int b[2][2] = {{5,6},{7,8}};
...
which I now want to pass to a function. I do not want to copy the 2D arrays into 3D array. However, I want to specify the bounds so that the function knows the dimensions of the 2D arrays so that I can index them conveniently with [i][j]
How can I format the functions' signature so that it accepts a pointer to an array (of unknown length) which contains 2D arrays of which it does now the dimensions?
E.g. something like
void myfunc(int[][3] *test, int len)
though of course this is syntactically invalid. Is specifying the bounds of arrays inside an array (passed by pointer) impossible in C? Will I be forced to move a and b into pointers, or forced to copy them into a 3D array?
If your compiler supports a variable length arrays you can write
void myfunc( int rows, int cols, int a[rows][cols] );
Take into account that the third parameter is implicitly converted to the type int ( * )[cols], that is within a function you are dealing with a pointer to one dimensional array. Nevertheless you can use expressions like
for ( int i = 0; i < rows; i++ )
{
for ( int j = 0; j < cols; j++ ) a[i][j] = value;
}
Otherwise if the compiler does not support variable length arrays and the second dimension of all arrays is the same then the function can be declared like
void myfunc( int ( *a )[2], int rows );
Take into account that this declaration
int[][3] *test
in any case is incorrect.
If you want to pass several two-dimensional arrays then you can within main declare a one dimensional array like
int a[2][2] = {{1,2},{3,4}};
int b[2][2] = {{5,6},{7,8}};
//...
int ( *p[] )[2] = { a, b, /*...*/ };
and then pass it to a function.
In this case the function will look like
void myfunc( int ( **p )[2], size_t n );
Here is a demonstrative program
#include <stdio.h>
void myfunc( int ( **p )[2], size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < 2; j++ )
{
for ( size_t k = 0; k < 2; k++ ) printf( "%d ", p[i][j][k] );
putchar( '\n' );
}
putchar( '\n' );
}
}
int main(void)
{
int a[2][2] = {{1,2},{3,4}};
int b[2][2] = {{5,6},{7,8}};
int ( *p[] )[2] = { a, b };
myfunc( p, sizeof( p ) / sizeof( *p ) );
return 0;
}
Its output is
1 2
3 4
5 6
7 8
If the first dimension of the arrays is not fixed and varies then you could pass also to the function an array of first dimensions of the arrays
If you have two things you need to pass to a function, you either
pass two separate arguments; or
create some kind of data structure that contains (pointers to) said things, and pass (a pointer to) that.
It doesn't matter if your things are arrays or anything else.
The same thing holds when you have a variable number of things. You can pass a variable number of arguments to a function, but that's a separate topic, so let's concentrate in option 2. In this case your data structure should be an array of pointers to things.
OK so how do you create one when your thing has a complex type, like an array (of arrays of pointers to functions that return a pointer to an array, or whatever)? The answer is simple: use a typedef.
typedef int MyThing[2][2]; // could be anything
MyThing one = {{1,2},{3,4}};
MyThing two = ...; // etc
MyThing* manyPointersToThings[] = {&one, &two};
void myFunc(int nThings, MyThing things[nThings]) {
// in here, each things[i] is a *pointer* to MyThing
(*things[0])[1][2] = 42;
// whatever
}
This works for any kind of thing. If your thing is in fact an array, there is another option: your data structure could store pointers to first elements of your arrays, rather than pointers to arrays themselves.
typedef int MyThing[2]; // this type names an *element* of your array
MyThing one[2] = {{1,2},{3,4}};
MyThing two[2] = ...; // etc
MyThing* manyPointersToThings[] = {one, two}; // note no & here
void myFunc(int nThings, MyThing things[nThings]) {
// in here, each things[i] is a pointer to the first element of an array
things[0][1][2] = 42;
// whatever
}
With this option, you gain some flexibility, as your arrays need not all be of the same size. You also lose that ugly dereference in parentheses.
For completeness, here are prototypes of the same functions sans typedef:
void myFunc(int nThings, int (*things[nThings])[2][2]) // version 1
void myFunc(int nThings, int (*things[nThings])[2]) // version 2
These are a bit more flexible than typedef versions, because now you can use a variable (another parameter) instead of the hardcoded number 2.
If you have trouble writing down things like the above, try this.

Trouble working with 2d array passed by reference C

So I am working on an assignment and I am having trouble figuring out how to use this 2d array which was passed by reference.
What I am given is this
int main(){
//cap and flow initialized
maximum_flow(1000, &(cap[0][0]), &(flow[0][0]));
}
So I wanted to copy the contents of cap over to another 2d array I dynamically allocated, but after hitting an error I decided to print out the values I have in cap2 and capacity, I'm not getting back all the values that I should.
void maximum_flow(int n, int *capacity, int *flow){
int **cap2;
cap2 = (int**) malloc(sizeof(int *)*n);
for (i = 0; i < n; i++)
{
cap2[i] = (int*) malloc(sizeof(int)*n);
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cap2[i][j] = (*(capacity + i*n + j));
(*(flow + i*n + j)) = 0;
}
}
}
This isn't going to be a terribly useful answer, since your code doesn't actually show the problem described; based on what's presented, I see no obvious reason why cap and cap2 shouldn't have the same contents by the end of the maximum_flow function. But I'd like to offer some background and a suggestion.
I'm going to assume cap and flow are declared as n by n arrays of int in main, where n is known at compile time.
The reason your instructor is using this interface is that passing multidimensional arrays as function arguments is problematic in C. Remember that unless it's the operand of the sizeof or unary & operators, or is a string literal being used to initialize another array in a declaraiton, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element of the array.
So, assuming a declaration like
int cap[10][10];
int flow[10][10];
the expressions cap and flow will each "decay" to type int (*)[10] (pointer to 10-element array of int). So if you wrote your function call as
maximum_flow( 1000, cap, flow );
then the function definition would have to be written as
void maximum_flow( int n, int (*cap)[10], int (*flow)[10] ) { ... }
or
void maximum_flow( int n, int cap[][10], int flow[][10] ) { ... }
In the context of a function parameter declaration, T a[][N] and T (*a)[N] mean the same thing.
The size of the outer dimension has to be specified in the array pointer declaration, and the problem is that a pointer to a 10-element array is a different, incompatible type from a pointer to an any-value-other-than-10-element array; thus, maximum_flow could only ever be used for N x 10-element arrays, limiting its usefulness. One way around this problem is to have the function receive an explicit pointer to the first element, and treat that pointer as a 1D array of size N * M.
Long story short, since you're treating your input parameters as 1D arrays, you are probably better off creating cap2 as a 1D array as well:
int *cap2 = malloc( sizeof *cap2 * n * n );
...
cap2[i * n + j] = capacity[i * n + j]; // use array subscript notation instead
flow[i * n + j] = 0; // of explicit dereferences
From the code you've posted, it's not clear what maximum_flow is supposed to do, nor why you need cap2. Note also that at some point you need to free the memory allocated to cap2, otherwise you have a memory leak.
If you're using a C99 or later compiler, you should be able to use a variable-length array instead of malloc:
int cap2[n * n]; // or int cap2[n][n], but like I said above, if you're
// treating your inputs as 1D arrays, you should also treat
// cap2 as a 1D array.
The advantage of a VLA is that you don't need to know the size at compile time, and it's treated like any other auto variable, meaning the memory for it will be released when the function exits.
The disadvantage of a VLA is that you can't use it as anything but a local variable; you can't have a VLA as a struct or union member, nor can you declare one static or at file scope. Neither can you explicitly initialize a VLA.

Pointer to a 2-D array

Please, I am new to C programming, please help me this.There is some error in the following code?
Basically, how do we work with pointers to 2-D arrays...
#include<stdio.h>
#include<stdlib.h>
int* max5(int** p,int r,int c)
{
/* have to design a function which takes as argument pointer to a 2-D array and returns a pointer to an array which contains the 5 highest entries*/
}
int main()
{
int rows,coloumns;
printf("Enter the number of rows and coloumns separated by a space\n");
scanf("%d %d",&rows,&coloumns);
int **ptr;
ptr=(int**)malloc(sizeof(int*)*rows);
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<coloumns;j++)
{
scanf("%d",*(ptr+i)+j);
printf("%d\n",*(*(ptr+i)+j));
}
}
free(ptr);
return 0;
}
In C, a 2D array is simply a regular array with different indexing. So, you have two options:
Allocate an array and index it as: array[I*n+j]
Allocate an array of arrays (which is what you seem to be trying to do)
The second you can achieve in the way shown in the related post: allocate matrix in C.
Dynamically allocating a 2D array can be done in several ways.
Assuming you have a C99 compiler or C2011 compiler that supports variable-length arrays, this is dead easy:
int rows, columns;
...
scanf("%d %d",&rows,&columns);
int array[rows][columns];
array is a variable-length array (VLA), meaning its dimensions are established at run time. To pass this to another function, the prototype would simply be
int *max5( int rows, int columns, int arr[rows][columns] ) { ... }
which can also be written as
int *max5( int rows, int columns, int (*arr)[columns] ) { ... }
I'll get into that second form below. Note that both rows and columns must be declared before you use them in the VLA declaraion.
You would call it as
int *maxarr = max5( rows, columns, array );
The advantage of this approach is that a) it's easy, and b) the memory for array is allocated contiguously (i.e., as a single, continuous block of memory) and released as soon as you exit it's enclosing scope; there's no need to manually free it. The disadvantage of this approach is that rows and columns can't be arbitrarily large, you can't initialize the array using regular initializer syntax (i.e., you can't write int array[rows][columns] = { ... };), and you can't define VLAs at file scope (i.e., as a global variable). There's also the problem that VLA support has always been a little spotty, and the 2011 standard now makes them optional, so it's not guaranteed they'll be supported everywhere.
If rows or columns is very large, or if for some other reason you want to allocate this memory from the heap, it's just slightly more complicated:
int rows, columns;
...
scanf("%d %d", rows, columns);
int (*array)[columns] = malloc( sizeof *array * rows );
In this version, array is a pointer to an N-element array of int (where N == columns), and we malloc space to hold M such arrays (where M == rows). Again, this takes advantage of VLA syntax, but instead of allocating the memory from the stack frame, we allocate it from the heap. Like in the first method, the memory is allocated contiguously. You would index into it as you would any other array:
array[i][j] = x;
When you're done with the array, you would free it as
free( array );
The prototype for max5 would be
int *max5( int rows, int columns, int (*arr)[columns] ) { ... }
Look familiar? That's the second version we used earlier when we were passing a 2D VLA.
Except when it is the operand of the sizeof or unary & operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be converted to an expression of type "pointer to T", and the value of the expression will be the address of the first element in the array.
In the first case where we declared array as a 2D VLA, when you call
int *maxarr = max5( rows, columns, array );
the expression array is converted from type "rows-element array of columns-element array of int" to "pointer to columns-element array of int", so what max5 receives is a pointer value, not an array. It just so happens that in the context of a function parameter declaration, T a[N] and T a[] are both interpreted as T *a; IOW, all three declare a as a pointer to T.
Thus, both
int *max5( int rows, int columns, int arr[rows][columns] ) { ... }
and
int *max5( int rows, int columns, int (*arr)[columns] ) { ... }
declare arr as a pointer to an array, not a 2D array. Similarly, both return a simple int *, as opposed to an array of int. The reason for this is simple; if you return a subarray using
return arr[i];
the expression arr[i] will have type "columns-element array of int"; by the rule above, that expression will be converted to type int *. C doesn't allow you to return array objects as arrays, nor can an array expression be the target of an assignment.
Now, what if you're working with a C89 compiler, or a C2011 compiler that doesn't support VLAs? In this case, you'd need to allocate the memory in a piecemeal fashion, like so:
int rows, columns;
int **array;
...
scanf("%d %d", rows, columns);
array = malloc( sizeof *array * rows );
if ( array )
{
int i;
for ( i = 0; i < rows; i++ )
{
array[i] = malloc( sizeof *array[i] * columns );
}
}
Your max5 prototype would now be
int *max5( int rows, int columns, int **arr );
You're not dealing with arrays or pointers to arrays anymore; you're dealing with a double pointer, which is not the same thing as a pointer to a 2D array. You can still subscript it as though it were a 2D array
arr[i][j] = x;
but it is not an array object in and of itself.
The most important thing to realize is that the memory is not guaranteed to be contiguous; rows may not be adjacent in memory, so you don't want to try to walk down rows using a pointer, like so:
int (*p)[columns] = arr;
while ( some_condition )
p++; // sets p to point to the next row
For an array that was allocated piecemeal, that sort of algorithm won't work.
Also, since the memory was allocated piecemeal, it needs to be freed piecemeal:
for ( i = 0; i < rows; i++ )
free(array[i]);
free( array );
Hope that helps.
this code fix your issue :
#include<stdio.h>
#include<stdlib.h>
int* max5(int** p,int r,int c)
{
/* have to design a function which takes as argument pointer to a 2-D array and returns a pointer to an array which contains the 5 highest entries*/
}
int main()
{
int rows,coloumns;
printf("Enter the number of rows and coloumns separated by a space\n");
scanf("%d %d",&rows,&coloumns);
int **ptr;
int i,j;
ptr=(int**)malloc(sizeof(int*)*rows);
for(j=0;j<coloumns;j++)
{
*(ptr+j) = (int *)malloc(sizeof(int)*coloumns);
}
for(i=0;i<rows;i++)
{
for(j=0;j<coloumns;j++)
{
scanf("%d",*(ptr+i)+j);
//*(*(ptr+j)+i) = i+j;
printf("%d\n",*(*(ptr+i)+j));
}
}
free(ptr);
return 0;
}
in fact you forget to allocate memory for columns !
You can allocate a 2D array in C by defining the type correctly:
int (*ptr)[coloumns] = calloc(sizeof(int) , rows * coloumns);
Then you can address it via ptr[i][j]. If your array does need to persist beyond the scope that allocated it and is fairly small then you can also allocate it on the stack
int ptr[rows][coloumns];
memset(ptr, 0, sizeof(ptr));
What you are trying to allocate is an array of pointers int (*)[coloumns] is not the same type as int ** although they can be both addressed through the use of the indexing operator [i][j]. The latter is a far more complicated data structure and is prone to more errors. I would advise against it unless you need variable row sizes.

Passing dynamically allocated array as a parameter in C

So... I have a dynamically allocated array on my main:
int main()
{
int *array;
int len;
array = (int *) malloc(len * sizeof(int));
...
return EXIT_SUCCESS;
}
I also wanna build a function that does something with this dynamically allocated array.
So far my function is:
void myFunction(int array[], ...)
{
array[position] = value;
}
If I declare it as:
void myFunction(int *array, ...);
Will I still be able to do:
array[position] = value;
Or I will have to do:
*array[position] = value;
...?
Also, if I am working with a dynamically allocated matrix, which one is the correct way to declare the function prototype:
void myFunction(int matrix[][], ...);
Or
void myFunction(int **matrix, ...);
...?
If I declare it as:
void myFunction(int *array, ...);
Will I still be able to do:
array[position] = value;
Yes - this is legal syntax.
Also, if I am working with a dynamically allocated matrix, which one
is correct to declare the function prototype:
void myFunction(int matrix[][], ...);
Or
void myFunction(int **matrix, ...);
...?
If you're working with more than one dimension, you'll have to declare the size of all but the first dimension in the function declaration, like so:
void myFunction(int matrix[][100], ...);
This syntax won't do what you think it does:
void myFunction(int **matrix, ...);
matrix[i][j] = ...
This declares a parameter named matrix that is a pointer to a pointer to int; attempting to dereference using matrix[i][j] will likely cause a segmentation fault.
This is one of the many difficulties of working with a multi-dimensional array in C.
Here is a helpful SO question addressing this topic:
Define a matrix and pass it to a function in C
Yes, please use array[position], even if the parameter type is int *array. The alternative you gave (*array[position]) is actually invalid in this case since the [] operator takes precedence over the * operator, making it equivalent to *(array[position]) which is trying to dereference the value of a[position], not it's address.
It gets a little more complicated for multi-dimensional arrays but you can do it:
int m = 10, n = 5;
int matrixOnStack[m][n];
matrixOnStack[0][0] = 0; // OK
matrixOnStack[m-1][n-1] = 0; // OK
// matrixOnStack[10][5] = 0; // Not OK. Compiler may not complain
// but nearby data structures might.
int (*matrixInHeap)[n] = malloc(sizeof(int[m][n]));
matrixInHeap[0][0] = 0; // OK
matrixInHeap[m-1][n-1] = 0; // OK
// matrixInHeap[10][5] = 0; // Not OK. coloring outside the lines again.
The way the matrixInHeap declaration should be interpreted is that the 'thing' pointed to by matrixInHeap is an array of n int values, so sizeof(*matrixInHeap) == n * sizeof(int), or the size of an entire row in the matrix. matrixInHeap[2][4] works because matrixInHeap[2] is advancing the address matrixInHeap by 2 * sizeof(*matrixInHeap), which skips two full rows of n integers, resulting in the address of the 3rd row, and then the final [4] selects the fifth element from the third row. (remember that array indices start at 0 and not 1)
You can use the same type when pointing to normal multidimensional c-arrays, (assuming you already know the size):
int (*matrixPointer)[n] = matrixOnStack || matrixInHeap;
Now lets say you want to have a function that takes one of these variably sized matrices as a parameter. When the variables were declared earlier the type had some information about the size (both dimensions in the stack example, and the last dimension n in the heap example). So the parameter type in the function definition is going to need that n value, which we can actually do, as long as we include it as a separate parameter, defining the function like this:
void fillWithZeros(int m, int n, int (*matrix)[n]) {
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
matrix[i][j] = 0;
}
If we don't need the m value inside the function, we could leave it out entirely, just as long as we keep n:
bool isZeroAtLocation(int n, int (*matrix)[n], int i, int j) {
return matrix[i][j] == 0;
}
And then we just include the size when calling the functions:
fillWithZeros(m, n, matrixPointer);
assert(isZeroAtLocation(n, matrixPointer, 0, 0));
It may feel a little like we're doing the compilers work for it, especially in cases where we don't use n inside the function body at all (or only as a parameter to similar functions), but at least it works.
One last point regarding readability: using malloc(sizeof(int[len])) is equivalent to malloc(len * sizeof(int)) (and anybody who tells you otherwise doesn't understand structure padding in c) but the first way of writing it makes it obvious to the reader that we are talking about an array. The same goes for malloc(sizeof(int[m][n])) and malloc(m * n * sizeof(int)).
Will I still be able to do:
array[position] = value;
Yes, because the index operator p[i] is 100% identical to *(ptr + i). You can in fact write 5[array] instead of array[5] and it will still work. In C arrays are actually just pointers. The only thing that makes an array definition different from a pointer is, that if you take a sizeof of a "true" array identifier, it gives you the actual storage size allocates, while taking the sizeof of a pointer will just give you the size of the pointer, which is usually the system's integer size (can be different though).
Also, if I am working with a dynamically allocated matrix, which one is the correct way to declare the function prototype: (…)
Neither of them because those are arrays of pointers to arrays, which can be non-contigous. For performance reasons you want matrices to be contiguous. So you just write
void foo(int matrix[])
and internally calculate the right offset, like
matrix[width*j + i]
Note that writing this using the bracket syntax looks weird. Also take note that if you take the sizeof of an pointer or an "array of unspecified length" function parameter you'll get the size of a pointer.
No, you'd just keep using array[position] = value.
In the end, there's no real difference whether you're declaring a parameter as int *something or int something[]. Both will work, because an array definition is just some hidden pointer math.
However, there's is one difference regarding how code can be understood:
int array[] always denotes an array (it might be just one element long though).
int *pointer however could be a pointer to a single integer or a whole array of integers.
As far as addressing/representation goes: pointer == array == &array[0]
If you're working with multiple dimensions, things are a little bit different, because C forces you declare the last dimension, if you're defining multidimensional arrays explicitly:
int **myStuff1; // valid
int *myStuff2[]; // valid
int myStuff3[][]; // invalid
int myStuff4[][5]; // valid

Passing an array of arrays in C

I need to have a function which takes a 2D array and generates random bits, so the result is an array of random binary strings.
I have the following code,
#define pop_size 50
#define chrom_length 50
main() {
int population[pop_size][chrom_length];
init_pop(&population);
}
int init_pop(int *population[][]) {
for(i = 0; i < pop_size; i++) {
for(j = 0; j < chrom_length; j++) {
*population[i][j] = rand() % 2;
}
}
return 0;
}
On compilation, I am getting the following error message:
array type has incomplete element type
Any suggestions?
Time for the usual spiel...
When an array expression appears in most contexts, its type is implicitly converted from "N-element array of T" to "pointer to T", and its value is set to point to the first element of the array. The exceptions to this rule are when the array expression is an operand of either sizeof or the unary & operators, or if it is a string literal being used as an initializer in a declaration.
What does all that mean in the context of your code?
The type of the expression population is "pop_size-element array of chrome_length-element arrays of int". Going by the rule above, in most contexts the expression population will implicitly be converted to type "pointer to chrome_length-element arrays of int", or int (*)[chrome_length].
The type of the expression &population, however, is "pointer to pop_size-element array of chrome_length-element arrays of int", or int (*)[pop_length][chrome_size], since population is an operand of the unary & operator.
Note that the two expressions have the same value (the address of the first element of the array), but different types.
Based on the code you've written, where you call the function as
init_pop(&population);
the corresponding function definition should be
int init_pop(int (*population)[pop_size][chrome_length]) // note that both dimensions
// must be specified
and you would access each element as
(*population)[i][j] = initial_value;
Note that this means init_pop can only deal with pop_size x chrome_length arrays; you can't use it on arrays of different sizes.
If you call the function as
init_pop(population); // note no & operator
then the corresponding function definition would have to be
int init_pop(int (*population)[chrome_length]) // or population[][chrome_length],
// which is equivalent
and you would access each element as
population[i][j] = initial_value;
Note that you don't have to dereference population explicitly in this case. Now you can deal with arrays that have different population sizes, but you're still stuck with fixed chromosome lengths.
A third approach is to explicitly pass a pointer to the first element of the array as a simple pointer to int and treat it as a 1D array, manually computing the offsets based on the array dimensions (passed as separate parameters):
init_pop(&population[0][0], pop_size, chrome_length);
...
int init_pop(int *population, size_t pop_size, size_t chrome_length)
{
size_t i, j;
...
population[i*chrome_length+j] = initial_value;
...
}
Now init_pop can be used on 2D arrays of int of different sizes:
int pop1[10][10];
int pop2[15][20];
int pop3[100][10];
...
init_pop(&pop1[0][0], 10, 10);
init_pop(&pop2[0][0], 15, 20);
init_pop(&pop3[0][0], 100, 10);
...
EDIT: Note that the above trick only works with contiguously allocated 2D arrays; it won't work with dynamically allocated arrays where the major dimension and the minor dimensions are allocated separately.
Here's a handy table, assuming a definition of int a[N][M]:
Expression Type Implicitly converted to
---------- ---- -----------------------
a int [N][M] int (*)[M]
a[i] int [M] int *
a[i][j] int
&a int (*)[N][M]
You need to tell the compiler all dimensions except the first, when passing arrays as arguments:
int init_pop(int population[][pop_size])
{
...
}
Yes, this means it's hard to make it completely dynamic and introduces a place where you have to repeat yourself.
UPDATE: I was confused, and had the requirement inverted. Fixed now.
For multidimensional arrays in C/C++ you have to specify all dimensions except the first.I am modifying your program to make it work correctly:
#include <stdio.h>
#include <stdlib.h>
#define pop_size 3
#define chrom_length 3
void init_pop(int population[][chrom_length]) {
int i,j;
for(i = 0; i < pop_size; i++) {
for(j = 0; j < chrom_length; j++) {
population[i][j] = rand() % 2;
}
}
}
/* For Checking */
void display (int population[][chrom_length]){
int i,j;
for(i = 0; i < pop_size; i++) {
for(j = 0; j < chrom_length; j++) {
printf("%d ",population[i][j]);
}
printf("\n");
}
}
int main(void) {
int population[pop_size][chrom_length];
init_pop(population);
display(population); /* For Checking */
return 0;
}
If you not going to use global constants here is the correct way of doing it.
This is the problem:
int *population[][]
A multidimensional array is simply a block of continuous memory, and when you say foo[3][2], the compiler finds the right index by 3*last_dimension_size + 2, which means that it has to know the size of all the dimensions except the last one.
So that declaration is an error.
BTW-- There are several very complete discussion of issues related to multidimensional arrays in c already on SO. Try searching under both or either [c] and [c++]

Resources