Segmentation fault shmat() with 2d array - arrays

Trying to allocate 2d array in shared memory. Execution returns segmentation fault during assignments (I think). Should I do the assignments in another way? This is my code:
...
#define KEYSM 46378
#define X 10
#define Y 10
int main(){
int i, j;
int shm_id;
int **addressArray;
if((shm_id = shmget(KEYSM, sizeof(int[X][Y]), IPC_CREAT | 0666)) == -1){
perror("shmget");
exit(-1);
}
if((addressArray = (int **)shmat(shm_id, NULL, 0)) == (int **)-1){
perror("shmat");
exit(-1);
}
for(i = 0; i < X; i++){
for(j = 0; j < Y; j++){
if(i % 2 != 0)
addressArray[i][j] = -1;
else
addressArray[i][j] = 0;
}
}
...
}

Your problem is in a – (your ?) – misunderstanding of the difference of a true 2D array, and a pointer→pointer→value indirection.
When you define a int **a this is interpreted as a pointer to another pointer, that then reference an int. Assigning a pointer obtained from an allocation function like malloc or shmat to such a double pointer, then as far as the semantics of C go, it expects this allocation of memory contain further pointers. So if you do a double dereference, and there's not a valid pointer there, it will fall flat on its face.
This misunderstanding is furthered by the fact, that in C you can validly write int a[X][Y]; and dereference it with a[i][j]. The key insight to understand this is, that the first "half", i.e. that with a array defined like that, a[i]… decays into a int* pointer, that points toward the 0th element in the i "column". The other half …[j] then dereferences this implicitly "appearing" pointer.
Multidimensional arrays in C are deceiving and I strongly discourage using them like that. Also you'll have a hard time to properly implement specific padding and row alignments with them, without jumping some really annoying arithmetic.
It's easier by far, to just write down the calculations explicitly, with the added benefit of having precise control of padding and alignment.
Suppose we want to create a 2D array of int, that shall be aligned to the sizes of long long
size_t const array_height = ...;
size_t const array_width = ...;
size_t const alignment = sizeof(long long);
size_t const row_size = array_width * sizeof(int);
size_t const row_stride =
alignment * ((row_size + alignment-1) / alignment);
size_t const array_size = array_height * row_stride;
int const shm_id = shmget(KEYSM, array_size, IPC_CREAT | 0666);
if( 0 > shm_id ){
perror("shmget");
exit(-1);
}
int *const array = shmat(shm_id, NULL, 0);
if( (void*)-1 == array ){
perror("shmat");
exit(-1);
}
for(size_t j = 0; j < array_height; ++j){
int *const row = array + j * row_stride;
for(size_t i = 0; i < array_width; ++i){
row[i] = (i % 2) ? -1 : 0;
}
}

A segfault is a runtime error, but your code shouldn't even compile, look a this statement (paranthesis missing):
if ( ( addressArray = ( int** ) shmat ( shm_id, NULL, 0 ) == ( int** ) -1 )
And what if that assignment failed? Does perror terminate the process? No. But you still access the array below. Most likely the source of the seg fault.
As mch pointed out:
// i is not declared
for(i = 0; i < X; i++){
// j is not declared, after j < Y, use semicolon instead of comma
for(j = 0; j < Y, j++){
// if the result is not zero you're doing an assignment
if(i % 2 != 0)
addressArray[i][j] = -1;
// but immediately after you assign a zero,
// previous statement -> useless
// 'else' is missing here
addressArray[i][j] = 0;
}
}

Related

parse 2d dynamic int array to Shared Memory

Im trying to build a program that parsing a 2d dynamic array to other program by using shared memory.I search a lot but im a bit confused because im not familiar at this one.
My code so far:
int main (int argc, char* argv []){
int rows,columns;
if( argc < 3 ){
printf("Need The size of the 2d array\n");
return 0;
}
rows = atoi(argv[1]);
columns = atoi(argv[2]);
time_t t;
srand((unsigned) time(&t));
key_t key = ftok(".",'a');
size_t size = sizeof(key_t) + (rows * columns + 2 + rows) * sizeof(int);
int shmid = shmget(key,size,IPC_CREAT|IPC_EXCL|S_IRWXU);
int *memory = shmat(shmid, NULL, 0);
printf("Shared Memory Key: %d\n", key);
int *argsflag = memory;
int *resflag= memory + 1;
int *res = memory + 2;
int **array = (int **) memory + (rows*columns);
for(int i = 0; i < rows ; i++) {
for(int j = 0; j < columns; j++) {
array[i][j] = rand() % 100;
}
}
for(int i = 0; i < rows ; i++) {
for(int j = 0; j < columns; j++) {
printf("%d ",array[i][j]);
}
printf("\n");
}
shmctl(shmid,IPC_RMID,NULL);
shmdt(memory);
return(0);
}
Im getting a Segmentation fault (core dumped) and i dont know why.Also by searching i find a solution with struct but i dint get how i can build that.
You cannot have a int** point at a 2D array. It can only point to the first element in a 1D array of int*.
Furthermore, what's the logic of memory + (rows*columns)? You end up setting the pointer to the last item of the array, rather than the first.
Try this instead:
void* memory = shmat( ...
...
int (*array)[columns] = memory;
...
array[i][j] = ... ;
Where int (*array)[columns] is an array pointer, which ends up point at the first array in the 2D array.
For details, see Correctly allocating multi-dimensional arrays.

populating array without calloc and malloc in c?

I have code that looks like
int main(int argc, char *argv[]){
char ***matrix;
matrix = calloc(2, sizeof(char **));
for (int i=0; i<2; i++){
matrix[i] = calloc(ROWS+2, sizeof(char*));
for (int j=0; j<COLS; j++){
matrix[i][j] = malloc(COLS+2);
memset(matrix[i][j], (int)DEFAULT, COLS+2);
}
}
Is there a way to do a similar kind of thing without the use of malloc and calloc? For example, in the case of 1d array I know you can do something like this
unsigned char malloc_data[MAX_SIZE];
size_t malloc_used; /* automatically initialized to zero */
void *stack_malloc(size_t size) {
void *p = &malloc_data[malloc_used];
if(size + malloc_used > MAX_SIZE) return 0; /* out of memory */
malloc_used += size;
return p;
}
OR something like this
static char *allocp = allocbuf[0];
if (allocbuf + ALLOCSIZE - allocp >= n) { /* it fits */
and
if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
I want to try to use stack variables and temps instead of dynamic memory allocated ways. However, when I try to think about how to apply these types of ways to my three dimensional sense, my head starts spinning. Please help clarify things to me
Assuming you want to keep the triple pointer usage, the storage can be allocated on the stack and initialized like this:
char ***matrix;
char **mats_[2];
char *mat_rows_[2][ROWS+2];
char mat_cols_[2][ROWS+2][COLS+2];
matrix = &mats_[0];
for (int i = 0; i < 2; i++) {
matrix[i] = &mat_rows_[i][0];
for (int j = 0; j < ROWS+2; j++) {
matrix[i][j] = &mat_cols_[i][j][0];
memset(matrix[i][j], (int)DEFAULT, COLS+2);
}
}
You could also define matrix as an array[2] of char ** instead of a single char ***, which would allow you to eliminate the mats_ variable:
char **matrix[2];
char *mat_rows_[2][ROWS+2];
char mat_cols_[2][ROWS+2][COLS+2];
for (int i = 0; i < 2; i++) {
...
In the comments below, OP asked whether the memset was necessary. All the elements of mat_cols_[2][ROWS+2][COLS+2] need to be set to the same value DEFAULT. If DEFAULT can be replaced with 0, this is easy to do by defining mat_cols_ with an initializer with pretty much everything defaulting to 0:
char mat_cols_[2][ROWS+2][COLS+2] = { 0 };
If DEFAULT cannot be replaced with 0, the only way to do it would be for the initializer to supply values for all the elements explicitly, which has a maintainability problem since the dimensions are defined in terms of the macros ROWS and COLS. The GNU C Compiler (GCC) has an extension to the C language designated initializers that allows a range of array elements to be initialized to the same value. That could be used as follows:
char mat_cols_[2][ROWS+2][COLS+2] =
{[0 ... 1] =
{[0 ... ROWS+2-1] =
{[0 ... COLS+2-1] = DEFAULT}
}
};

Returning multidimensional arrays from a function in C

What is the best way to return a multidimensional array from a function in c ?
Say we need to generate a multidimensional array in a function and call it in main, is it best to wrap it in a struct or just return a pointer to memory on the heap ?
int *create_array(int rows, int columns){
int array[rows][columns] = {0};
return array;
}
int main(){
int row = 10;
int columns = 2;
create_array(row,columns);
}
The code above, is just to sketch out the basic program I have in mind.
This is wrong:
int *create_array(int rows, int columns){
int array[rows][columns] = {0};
return array;
}
and should produce a warning like this:
prog.c:2:6: note: (near initialization for 'array')
prog.c:3:13: warning: return from incompatible pointer type [-Wincompatible-pointer-types]
return array;
^~~~~
prog.c:3:13: warning: function returns address of local variable [-Wreturn-local-addr]
since you are returning the address of an automatic variable; its lifetime ends when its corresponding function terminates.
You should either declare a double pointer in main(), pass it through the function, dynamically allocate memory for it and return that pointer. Or you could create the array in main() and pass the double pointer to the function.
I want to know ways to allocate multidimensional arrays on the heap and pass them around
For allocating memory on the heap you could use one of these two methods, which involve pointers:
#include <stdio.h>
#include <stdlib.h>
// We return the pointer
int **get(int N, int M) /* Allocate the array */
{
/* Check if allocation succeeded. (check for NULL pointer) */
int i, **array;
array = malloc(N*sizeof(int *));
for(i = 0 ; i < N ; i++)
array[i] = malloc( M*sizeof(int) );
return array;
}
// We don't return the pointer
void getNoReturn(int*** array, int N, int M) {
/* Check if allocation succeeded. (check for NULL pointer) */
int i;
*array = malloc(N*sizeof(int *));
for(i = 0 ; i < N ; i++)
(*array)[i] = malloc( M*sizeof(int) );
}
void fill(int** p, int N, int M) {
int i, j;
for(i = 0 ; i < N ; i++)
for(j = 0 ; j < M ; j++)
p[i][j] = j;
}
void print(int** p, int N, int M) {
int i, j;
for(i = 0 ; i < N ; i++)
for(j = 0 ; j < M ; j++)
printf("array[%d][%d] = %d\n", i, j, p[i][j]);
}
void freeArray(int** p, int N) {
int i;
for(i = 0 ; i < N ; i++)
free(p[i]);
free(p);
}
int main(void)
{
int **p;
//getNoReturn(&p, 2, 5);
p = get(2, 5);
fill(p ,2, 5);
print(p, 2, 5);
freeArray(p ,2);
return 0;
}
Pick whichever suits best your style.
What is the best way to return a multidimensional array from a function in c ?
My recommendation is to avoid doing that, and avoid multidimensional arrays in C (they are unreadable and troublesome).
I would recommend making your matrix type your proper abstract data type, represented by some struct ending with a flexible array member:
struct mymatrix_st {
unsigned nbrows, nbcolumns;
int values[];
};
Here is the creation function (returning a properly initialized pointer to dynamic memory):
struct mymatrix_st*
create_matrix(unsigned mnbrows, unsigned mnbcolumns) {
if (mnbrows > UINT_MAX/4 || mnbcolumns > UINT_MAX/4
||(unsigned long)mnbrows * (unsigned long)mnbcolums
> UINT_MAX) {
fprintf(stderr, "too big matrix\n");
exit(EXIT_FAILURE);
};
size_t sz = sizeof(struct mymatrix_st)+(mnbrows*mnbcolumns*sizeof(int));
struct mymatrix_st*m = malloc(sz);
if (!m) {
perror("malloc mymatrix"); exit(EXIT_FAILURE); };
m->nbrows = mnbrows;
m->nbcolumns = mnbcolumns;
for (unsigned long ix=(unsigned long)mnbrows * (unsigned long)mnbcolumns-1;
ix>=0; ix--)
m->values[ix] = 0;
return m;;
} /*end create_matrix*/
It is on purpose that struct mymatrix_st don't contain any interior pointer. You can and should use free to destroy it.
Here is the accessor function; make it a static inline function and define it in the same header declaring struct mymatrix_st and create_matrix, e.g.
static inline int getmatrix(struct mymatrix_st*m, unsigned row, unsigned col) {
if (!m) {
fprintf(stderr, "getmatrix with no matrix\n");
exit(EXIT_FAILURE);
};
if (row >= m->nbrows || col >= m->nbcolumns){
fprintf(stderr, "getmatrix out of bounds\n");
exit(EXIT_FAILURE);
};
return m->values[row*m->nbcolumns + col];
}
I leave up to you to define and implement the other operations on your abstract struct mymatrix_st type.
(you could adapt the code, perhaps removing the out of bound check, but I don't recommend unsafe code)
int** create_array(int rows, int columns){
int** array = malloc(rows * sizeof(int*));
int i;
for (i=0; i<rows; i++)
array[i] = malloc(columns * sizeof(int));
return array;
}
should do the trick. If you use int array[rows][columns]; then it's dead as soon as the functiom returns, and you get a UB. You should at least use dynamic memory allocation.
You can't return an array, but you can return a regular pointer and document that the callee may treat it as a pointer to a multidimensional array of the dimensions that it had passed to the caller.
(Note that the returned pointer must point to dynamic or static, but not automatic memory--don't return pointers to local variables!)
It takes some slightly wordy casts and possibly a macro but it's doable:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void*
multi(int R, int C)
{
return calloc ( 1, sizeof(int[R][C]) ); //or sizeof(int)*R*C
}
int main()
{
int (*r_)[3][4] = multi(3,4);
if(!r_) return EXIT_FAILURE;
#define r (*r_)
//emulate C++ a reference -- r now behaves as an `int r[3][4];`
//Test that addresses advance as they would in a multi-d array
int local[3][4];
assert(&local[1][0]-&local[0][0] == 4); //base example
assert(&r[1][0]-&r[0][0] == 4); //"returned" multi-d array
free(r); //or free(&r) or free(r_) -- here it shouldn't matter
#undef r
return 0;
}
Note that an array of pointers is not the same thing as a multi-d array.
A true multi-d array is one contiguous block, whereas an array of pointers (though usable with the same indexing syntax) has much worse locality of reference, so this might be preferable over returning pointers to pointers if you want better performance.

Storing and Accessing a 2D Array in a Struct

I am trying to code a program in C that generates a spiral based on user input and prints it to the console. I cannot figure out how to access the 2D array "data" that I defined in the struct "Spiral". How do I fix the "warning: assignment from incompatible pointer type" error?
#include <stdio.h>
typedef struct Spiral {
int size;
int **data;
} Spiral;
Spiral generateSpiral(int size);
void printSpiral(Spiral spiral);
static int rotate();
int main() {
int size;
scanf("%d", &size);
Spiral spiral = generateSpiral(size);
printSpiral(spiral);
return 0;
}
Spiral generateSpiral(int size) {
int data[size][size];
int i;
for (i = 0; i < size; i++) {
int j;
for (j = 0; j < size; j++) {
data[i][j] = 0;
}
}
for (i = 0; i < size; i++) {
data[0][i] = 1;
}
int currX = 0;
int currY = size - 1;
for (i = size - 1; i > 0; i -= 2) {
int j;
for (j = 0; j < 2; j++) {
int k;
switch (rotate()) {
case 0:
for (k = 0; k < i; k++) {
data[++currX][currY] = 1;
}
break;
case 1:
for (k = i; k > 0; k--) {
data[currX][--currY] = 1;
}
break;
case 2:
for (k = i; k > 0; k--) {
data[--currX][currY] = 1;
}
break;
case 3:
for (k = 0; k < i; k++) {
data[currX][++currY] = 1;
}
break;
}
}
}
Spiral spiral;
spiral.size = size;
spiral.data = data;
return spiral;
}
void printSpiral(Spiral spiral) {
int i;
for (i = 0; i < spiral.size; i++) {
int j;
for (j = 0; j < spiral.size; j++) {
switch (spiral.data[i][j]) {
case 0:
printf(" ");
break;
case 1:
printf("#");
break;
}
}
printf("\n");
}
}
static int rotate() {
static int val = 0;
int tmp = val;
val++;
if (val > 3)
val = 0;
return tmp;
}
In the generateSpiral function you make the structures pointer point to the local variable data, but when the function returns data goes out of scope so the pointer now points to unallocated memory leading to undefined behavior.
But that's not your only problem: A second problem is that a pointer to a pointer is not the same as an array of arrays, the memory layout is different.
For the last part, lets check an example. Lets say we have the following declaration
int a[2][2];
In memory it will look something like this:
+---------+---------+---------+---------+
| a[0][0] | a[0][1] | a[1][0] | a[1][1] |
+---------+---------+---------+---------+
In other words, all data is contiguous.
If you, on the other hand have a declaration like
int **p;
and allocate data for it correctly, it will look something like
+------+------+-----+
| p[0] | p[1] | ... |
+------+------+-----+
| | |
| | v
| | ...
| v
| +---------+---------+-----+
| | p[1][0] | p[1][1] | ... |
| +---------+---------+-----+
v
+---------+---------+-----+
| p[0][0] | p[0][1] | ... |
+---------+---------+-----+
The memory is no longer contiguous. There is no longer any maximum size, a pointer points to a contiguous area of memory, but there is no way of knowing how big that area is. You have to keep track of it yourself.
The simple solution to both the problems is to use only pointer to pointer, and then allocate dynamically of the heap:
int **data;
// First allocate memory for `size` number of pointers
// I.e. we allocate an "array" of pointers
data = malloc(size * sizeof(int *));
// Then allocate each entry in the above allocated "array"
// I.e. make each pointer in the "array" point to an "array" of `int`
for (int i = 0; i < size; ++i)
data[i] = malloc(size * sizeof(int));
Now the local variable data can be used directly to assign to spiral.data.
But there is a catch: In Java you don't have to free memory you allocate, it's handled automatically. In C it's not handled automatically, you have to manually free the memory you allocate or you will have a memory leak.
Freeing the memory can be done like
// First free all the "sub-arrays"
for (int i = 0; i < size; ++i)
free(spiral.data[i]);
// Then free the top-level "array"
free(spiral.data);
Regarding pointers, a pointer can point to any memory address, and there is really no safety or checking that it points to a valid location. Also, C does not do deep copying of values and structures, if you have a pointer and make it point somewhere, then the compiler or runtime system doesn't make a copy.
And about scoping, Java has local variables as well, and just like in C when a function returns those go out of scope. The difference between Java and C, is that if you return a reference to a local object, then Java keeps track of that and keep the object in memory as long as there are references to the object. C doesn't have references, a pointer is just an integer whose value is an address in memory, and the data pointed to have no idea that there are pointers to it or how many.
Your problem is due to a mismatch between the int and the unsigned int returned by the vectors size function. Change your int i , j to type size_t so they are also an unsigned integer like the size of the vectors. This is why you are getting the warning

int** vs int[const][const] differences

I was writing a code the other day and I found it rather strange, that int** and int[][] does not behave the same way. Can anyone point out the differences between them? Below is my sample code, which fails with a segmentation fault, if I pass constant size 2d array, while it does work fine when I pass a dinamically allocated 2d array.
I am confused mainly because ant int[] array works the same as int*.
#include<stdio.h>
#include<stdlib.h>
void sort_by_first_row(int **t, int n, int m)
{
int i, j;
for(i = m-1 ; i > 0 ; --i)
{
for(j = 0 ; j < i; ++j)
{
if(t[0][j] < t[0][j+1])
{
int k;
for(k = 0 ; k < n ;++k)
{
int swap;
swap = t[k][j];
t[k][j] = t[k][j+1];
t[k][j+1] = swap;
}
}
}
}
}
int main(void) {
int i, j;
/* Working version */
/*int **t;
t =(int**) malloc(3*sizeof(int*));
for(i = 0; i < 3; ++i)
{
t[i] = (int*) malloc(6*sizeof(int));
}*/
/*WRONG*/
int t[3][6];
t[0][0] = 121;
t[0][1] = 85;
t[0][2] = 54;
t[0][3] = 89;
t[0][4] = 879;
t[0][5] = 11;
for( i = 0; i < 6; ++i )
t[1][i] = i+1;
t[2][0] = 2;
t[2][1] = 4;
t[2][2] = 5;
t[2][3] = 3;
t[2][4] = 1;
t[2][5] = 6;
sort_by_first_row(t, 3, 6);
for(i = 0; i < 3; ++i)
{
for(j = 0; j < 6; ++j)
printf("%d ", t[i][j]);
printf("\n");
}
return 0;
}
So based on the below answers I realize, that a multidimensional array is stored continuously in a row major order. After some modification, the below code works:
#include<stdio.h>
#include<stdlib.h>
void sort_by_first_row(int *t, int n, int m)
{
int i, j;
for(i = m-1 ; i > 0 ; --i)
{
for(j = 0 ; j < i; ++j)
{
if(t[j] < t[j+1])
{
int k;
for(k = 0 ; k < n ;++k)
{
int swap;
swap = t[k*m + j];
t[k*m + j] = t[k*m + j+1];
t[k*m + j+1] = swap;
}
}
}
}
}
int main(void) {
int i, j;
/* Working version */
/*int **t;
t =(int**) malloc(3*sizeof(int*));
for(i = 0; i < 3; ++i)
{
t[i] = (int*) malloc(6*sizeof(int));
}*/
/*WRONG*/
int t[3][6];
t[0][0] = 121;
t[0][1] = 85;
t[0][2] = 54;
t[0][3] = 89;
t[0][4] = 879;
t[0][5] = 11;
for( i = 0; i < 6; ++i )
t[1][i] = i+1;
t[2][0] = 2;
t[2][1] = 4;
t[2][2] = 5;
t[2][3] = 3;
t[2][4] = 1;
t[2][5] = 6;
sort_by_first_row(t, 3, 6);
for(i = 0; i < 3; ++i)
{
for(j = 0; j < 6; ++j)
printf("%d ", t[i][j]);
printf("\n");
}
return 0;
}
My new question is this: How to modify the code, so that the procedure works with int[][] and int** also?
Realize that int **t makes t a pointer to a pointer, while int t[3][6] makes t an array of an array. In most cases, when an array is used in an expression, it will become the value of the address of its first member. So, for int t[3][6], when t is passed to a function, the function will actually be getting the value of &t[0], which has type pointer to an array (in this case, int (*)[6]).
The type of what is being pointed at is important for how the pointer behaves when indexed. When a pointer to an object is incremented by 5, it points to the 5th object following the current object. Thus, for int **t, t + 5 will point to the 5th pointer, while for int (*t)[M], t + 5 will point to the 5th array. That is, the result of t + 5 is the same as the result of &t[5].
In your case, you have implemented void sort_by_first_row(int **t, int n, int m), but you are passing it an incompatible pointer. That is, the type of &t[0] (which is what t will become in main) is not the same as what the function wants, a int **t. Thus, when the sorting function starts to use that address, it will think its indexing into pointers, when the underlying structure is an array of arrays.
int** is quite different from int[][]. int** is simply a pointer to a pointer and would appear like the following:
in reality, you can access the entire multidimensional array with simply int* pointing to the first element, and doing simple math from that.
Here is the result of the separate allocations (in your commented code):
However when you allocate a multidimensional array, all of the memory is contiguous, and therefore easy to do simple math to reach the desired element.
int t[3][6];
int *t = (int*) malloc((3 * 6) * sizeof(int)); // <-- similarly
This will result in a contiguous chunk of memory for all elements.
You certainly can use the separate allocations, however you will need to walk the memory differently.
Hope this helps.
int t[3][6] is very nearly the same thing as int t[18]. A single contiguous block of 18 integers is allocated in both cases. The variable t provides the address of the start of this block of integers, just like the one-dimensional case.
Contrast this with the case you have marked as "working", where t gives you the address of a block of 3 pointers, each of which points to a block of memory with 6 integers. It's a totally different animal.
The difference between t[3][6] and t[18] is that the compiler remembers the size of each dimension of the array, and automatically converts 2D indices into 1D offsets. For example, the compiler automatically converts t[1][2] into *(t + 1*6 + 2) (equivalent to t[8] if it were declared as a one-dimensional array).
When you pass a multi-dimensional array to a function, there are two ways to handle it. The first is to declare the function argument as an array with known dimension sizes. The second is to treat your array like a 1D array.
If you are going to declare the size of your array, you would declare your function like this:
void sort_by_first_row(int t[][6], int n)
or this
void sort_by_first_row(int t[3][6])
You either have to declare all array dimension sizes, or you can leave out the first size. In both cases, you access elements of t using t[i][j]; you've given the compiler enough information to do the offset math that converts from 2D notation to a 1D index offset.
If you treat it as a 1D array, you have to pass the array dimensions and then do the offset math yourself.
Here's a full working example, where f and f2 both generate the same output:
void f(int* t, int m, int n)
{
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
std::cout << t[i * n + j] << " ";
std::cout << std::endl;
}
void f2(int t[][6], int m)
{
for (int i = 0; i < m; i++)
for (int j = 0; j < 6; j++)
std::cout << t[i][j] << " ";
std::cout << std::endl;
}
int main()
{
int t[3][6];
int val = 1;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 6; j++)
{
t[i][j] = val;
val++;
}
}
f(&(t[0][0]), 3, 6);
f2(t, 3);
return 0;
}
One thing to note is the hack-ish way I had to pass t to f. It's been a while since I wrote in C/C++, but I remember being able to pass t directly. Maybe somebody can fill me in on why my current compiler won't let me.
A int ** is a pointer to a pointer to an int, and can be a pointer to an array of pointers to arrays of ints. A int [][] is a 2-dimensional array of ints. A two-dimensional array is exactly the same as a one-dimensional array in C in one respect: It is fundamentally a pointer to the first object. The only difference is the accessing, a two-dimensional array is accessed with two different strides simultaneously.
Long story short, a int[][] is closer to an int* than an int**.

Resources