Matrices in C - subscripted value is neither array nor pointer
Hello, I'm struggling to use matrices in C although I've read so much about pointers and malloc in different literatures. However all my attempts fail, subscripted value is neither array nor pointer being the last.
What I want to do is have A as global 2D matrix (I know it's just an array) such that functions inside the same source code initialize, modify and print elements of it, by accessing A[i][j] (Java style). I'd read this to be possible, but when I try it doesn't work.
I also want the size to be allocated at run time.
Here is my understanding:
Code:
#include <stdlib.h>
#include <stdio.h>
int *A; //pointer
.... functions that access A[i][j] , and print.
int main(int argc, char * argv[]){
int n = (int) argv[1];
A = malloc(sizeof (int) * n * n); //allocation of memory to the array;
....call functions that operate on matrix.
}
This doesn't work. Could someone point me out to a solution (preferably giving exact code to achieve the same). I would like to not set an initial size of the array, at the times I declare the global pointer, yet if I had to I'll use realloc(A, n) in the main, but this also doesn't work with me. If you point out how to do that too I'll be grateful.