1

I'm trying to do something as followed

float* A = fill_float(); //in other words A is full
float aIn2D[n/p][n] = &A; //the 2d array should be able to alter the 1d

I tried the above but wouldn't compile (something about not being able to make the size variable length?). also tried

float** aIn2D = &A

But in retrospect that was nonsensical as how would the float** magically know I want a row, column distribution of [n/p][n] (i don't think it does since the program crashes right at the first access of aIn2D).

So does anyone know how to do something like float aIn2D[n/p][n] = &A; in c?

edit: One more thing, I for sure know the size from [n/p][n] is going to be the size of the amount of data A is holding.

edit2: A very big aspect is that both arrays point to the same memory location, aIn2D is just a way for me to easily access A in methods that follow.

Jaime
  • 73
  • 5

2 Answers2

1

Assuming n_rows and n_cols are variables, let's say you have an n_rows*n_cols 1D array of floats that you want to treat as an n_rows x n_cols 2D array.

With C99, which supports variable-sized arrays, you actually can do pretty much what you want:

float* A = fill_float();
float (*aIn2D)[n_cols] = (float (*)[n_cols])A;

Otherwise, you can do something like this:

float *A = fill_float();
float **aIn2D = malloc(n_rows*sizeof(float*));

{
  int i=0;
  for (; i!=n_rows; ++i) {
    aIn2D[i] = A + i*n_cols;
  }
}

// Use aIn2D[row][col]

free(aIn2D);
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
  • don't go with emulation of a 2D matrix when the language has real 2D matrices – Jens Gustedt Nov 29 '12 at 07:15
  • @JensGustedt: I've updated my answer to reflect how it can be done with C99. – Vaughn Cato Nov 29 '12 at 07:18
  • -1 For teaching to cast the result of malloc in C. Read [this](http://c-faq.com/malloc/mallocnocast.html) and [this](http://stackoverflow.com/questions/1565496/specifically-whats-dangerous-about-casting-the-result-of-malloc). – Lundin Nov 29 '12 at 07:31
  • @Lundin: that's cruel and unnecessary punishment. Casting the return from `malloc()` is not harmful if you code in C99 or later; you have to have a prototype for `malloc()` in scope. It can only cause any problems if you work in C89 and don't get warnings/errors on undeclared functions and don't include `` — which means you're being a sloppy coder and the cast on `malloc()` is probably the least of your problems anyway. – Jonathan Leffler Nov 29 '12 at 15:06
  • @JonathanLeffler There is absolutely no reason to cast the result of malloc, doing so only shows that you don't understand the pointer conversion rules in C, C99 or not. Most of the time, it is because people are attempting to compile C code on a C++ compiler, which is always a bad idea. Anyway, the OP has fixed it, so I removed the down vote, as per the policy of this site. Downvotes are supposedly meant to be used to educate, rather than as a punishment. – Lundin Nov 29 '12 at 15:36
  • @Lundin: We're going to have to agree to disagree, I guess. – Jonathan Leffler Nov 29 '12 at 16:47
0

If you want to convert 2-D array to 1-D then you should try like this :

float a[MAX];
float dup_a[ROW][COL];
int i,j,k;
for(i = 0 ; i < MAX ; i++)
{
  j= i / ROW ;  // you can do it by i / COL also 
  k= i % ROW ; // you can do it by i % COL also 
  dup_a[j][k] = a[i];
}

Or, you can try this ,

float* A = fill_float();
float (*aIn2D)[n] = &A; // create a pointer to an array 
Omkant
  • 9,018
  • 8
  • 39
  • 59