I have an array of float rtmp1[NMAX * 3][3], and it is used as rtmp1[i][n], where n is from 0 to 2, and i is from 0 to 3 * NMAX - 1. However, I would like to convert rtmp1 to be rtmp1[3 * 3 * NMAX]. Would addressing this new 1D array as rtmp1[3 * i + n] be equivalent to rtmp1[i][n]? Thanks in advance for the clarifications.
-
Shouldn't i be from 0 to (3 * NMAX) - 1 ?KTC– KTC2009-08-10 19:43:49 +00:00Commented Aug 10, 2009 at 19:43
-
Clarification please. Do you mean "i is from 0 to 2" or rtmp1[3 * 4 * NMAX]?b w– b w2009-08-10 19:45:10 +00:00Commented Aug 10, 2009 at 19:45
-
@KTC: yes, and I have corrected it @bill weaver: Please clarify your question.stanigator– stanigator2009-08-10 19:53:10 +00:00Commented Aug 10, 2009 at 19:53
-
I think the answer to your basic question is yes (see my answer below). However, i'm confused by your ranges in the first part of your question. After your edit, i'm more confused by them.b w– b w2009-08-10 20:08:04 +00:00Commented Aug 10, 2009 at 20:08
3 Answers
rtmp1[i][n] is equivalent to rtmp1[i*NMAX + n]
See http://www.cplusplus.com/doc/tutorial/arrays/, where your NMAX is their width.
1 Comment
Yes, but what are you trying to prove by doing this? rtmp1[i][n] would most likely have better execution time and is easier to read.
"Actually, you'd want to use rtmp[i + 3*n]" what is the difference? All you are doing is swapping addresses.
1 Comment
I'm not sure it won't break some alias prevention rules. My reading is that it is OK, but I've already been wrong and the whole area is sometimes confusing and bold on to know of which of two conflicting rule in different part of the standard takes priority.
Exemple:
typedef float Point[3];
void f(float* tab, Point* pt)
{
(*pt)[2] = 6;
// I don't think the compiler can assume that (*pt)[2] isn't modified by
tab[5] = 3.141592;
}
// context which give a problem if I'm wrong.
float rtmp1[NMAX*3][3];
float *ptr = &rtmpl[0][0];
f(ptr, rtmpl[1]);