I have an NxN 2D array implemented as a 1D array using A[N*i+j]. I want to make references to the columns of the array and be able to pass them to functions as regular column vectors. You can see an example below for N=3. The function returns just the square of the vector passed to it:
#include <stdio.h>
int func(int* vec[3]){
int result=0;
for(int i=0;i<3;i++){
result+=(*vec[i])*(*vec[i]);
printf("vec[%d]=%d\n",i,*vec[i]);
}
return result;
}
void main(){
int A[]={0,1,2,3,4,5,6,7,8};
int *a[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
a[j][i]=&A[3*i+j];
}
}
for(int i=0;i<3;i++){
printf("vector %d\n",i);
for(int j=0;j<3;j++){
printf("%d\n",*a[i][j]);
}
}
printf("%d\n",func(a[0]));
}
This works but the problem is the function works only for arguments of type int* vec[3]. What I would really like is to have the function taking an argument of type int vec[3] but I'm confused on how I should then pass the vector of pointers as a vector of the values the vector elements point to.