Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidimensional Arrays in a struct in C#

I'm trying to convert the following (shortened for readability) to C# and running into problems

#define DISTMAX 10
struct Distort {
  int    a_order;
  double a[DISTMAX][DISTMAX];
};

I thought in structs it was a simple case of using "fixed" however I'm still getting problems.

Here's what I've got (With a define higher up the page):

const int DISTMAX = 10;
struct Distort
{
        int a_order;
        fixed double a[DISTMAX,DISTMAX];
}

The error I get is stimply Syntax error that ] and [ are expected due to what I expect to be a limitation of a single dimension array.

Is there a way around this?

like image 718
John Avatar asked Mar 20 '09 10:03

John


People also ask

Can you have an array in a struct?

A structure may contain elements of different data types – int, char, float, double, etc. It may also contain an array as its member. Such an array is called an array within a structure. An array within a structure is a member of the structure and can be accessed just as we access other elements of the structure.

How are multidimensional arrays stored in C?

The data items in a multidimensional array are stored in the form of rows and columns. Also, the memory allocated for the multidimensional array is contiguous. So the elements in multidimensional arrays can be stored in linear storage using two methods i.e., row-major order or column-major order.

What is multi-dimensional array in data structure?

A multidimensional array associates each element in the array with multiple indexes. The most commonly used multidimensional array is the two-dimensional array, also known as a table or matrix. A two-dimensional array associates each of its elements with two indexes.

What is multidimensional array in C with example?

In C programming, you can create an array of arrays. These arrays are known as multidimensional arrays. For example, float x[3][4];


1 Answers

Fixed sized buffers can only be one-dimensional. You'll need to use:

unsafe struct Distort
{
     int a_order;
     fixed double a[DISTMAX * DISTMAX];
}

and then do appropriate arithmetic to get at individual values.

like image 176
Jon Skeet Avatar answered Oct 03 '22 10:10

Jon Skeet