The code:
vet = (int**)malloc( (N)*(N)*sizeof(int*) );
allocates space for n**2 pointers to integers.
The code:
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
scanf("%d", &vec[j]);
Tries to store N**2 integers (not pointers to integers) into a doubly dimensioned array, but the compiler has never been told the dimensions of that array. So, that can't work.
It looks like your code might be trying to set up an array of N pointers with each pointer pointing to a vector of N integers. But without seeing the code that defines check_property()
, I'm obviously just guessing. Assuming that the 1st argument to that function is supposed to be a pointer to an array of N integers (not an array of pointers to integers), the following modification of your program might give you something you can use as a starting point:
#include <stdio.h>
#include <stdlib.h>
char *IAm;
/* For this demo we dn't care about the argument being a power of 2; just > 0 */
int check(int N) {
return(N > 0);
}
/* Calculate subscript into a vector for a simulated NxN array. */
int s(int N, int row, int col) {
if(N < 0 || row < 0 || col < 0 || row >= N || col >= N) {
fprintf(stderr, "%s: s(N=%d,row=%d,col=%d)\n",
IAm, N, row, col);
fprintf(stderr, "%s: s(): Expect N>0, 0<=row<N, 0<=col<N\n",
IAm);
exit(4);
}
return(row * N + col);
}
int main(int argc, char *argv[]) {
int i,
j,
N,
result = 0,
*vec; /* Note *vec; not **vec. */
IAm = argv[0];
if (argc < 2) {
fprintf(stderr, "Usage: %s N\n", argv[0]);
exit(1);
}
/* ... how many numbers to read */
N = atoi(argv[1]);
/* check if N is a power of two; exit if not */
if (! check(N)) {
fprintf(stderr,
"%d is not a power of 2 as required. Exiting.\n", N);
exit(2);
}
vec = (int*)malloc( (N)*(N)*sizeof(int) ); /* int**->int*; int*->int */
if ( vec == NULL ) {
fprintf (stderr, "ERROR: not enough memory available!\n");
exit(3);
}
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
scanf("%d", &vec[s(N, i, j)]); /* [j]->[s(N,i,j)] */
printf("\nOriginal array:\n"); /* Trailing space removed from format.*/
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++)
printf("%d ", vec[s(N, i, j)]);
printf("\n");
}
/* Without knowing what check_property() does, I don't know if:
** check_property(vec, 0, N-1)
** in the following just needs to be changed to:
** check_property(&vec[i * N], 0, N-1)
** or if something much deeper needs to be done.
*/
/* Now process array; example below is to add numbers */
// for (i = 0; i < N; i++) {
// result = check_property(vec, 0, N-1);
// printf("result[line %d]: %d\n", i, result);
// }
/* free memory */
free(vec);
exit(0);
}
Obviously you need to replace the check()
function in this code with your own check()
function. And, obviously, you need to have something that pipes or redirects a file that contains N**2 strings representing integer values into this code. If you just invoke it as:
./program 16
your program will hang until you type in 256 integer values separated by whitespace characters and with at least the last integer value followed by a <newline>.
Note also that instead of using vec
as a doubly dimensioned array when reading values into it and printing the value stored in it, it treats it as a singly dimensioned array and gives the function s()
the dimensions of the array and the row and column values to get the offset in that array treated as though it were a doubly dimensioned array.
If you invoke it with:
echo 11 12 13 14 21 22 23 24 31 32 33 34 41 42 43 44 | ./program 4
it will produce the output:
Original array:
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44