1

How can I make array in existing array's index without using pointer for e.g

float[] currentNode = new float[12]
float[] neighbour = new float[12]

neighbour[8] = new float[12]
neighbour[8] = currentNode;

and can access with neighbour[8][1]

other option is something using pointers.

float *pointer;
int []array = new int[12];
pointer = &array[0];

neighbour[8] = pointer

so does first solution possible w/o changing my both arrays ? any other solutions ?? .

3 Answers 3

5

You are looking for Multidimensional arrays.

int[,] myArray;
myArray = new int[,] {{1,2}, {3,4}, {5,6}, {7,8}};   // OK
Sign up to request clarification or add additional context in comments.

Comments

2

You can't do that.

You have an array of float values, not an array of arrays.

Which means you cannot assign one element in the array (which holds a float value) a value which is an array.

You'd have to redefine your variables as:

float[][] neighbour = new float[12][];

This will declare an array of arrays, which means each element of the neighbour array can hold arrays of different lengths, or no array (null-reference).

If you want to declare a matrix, you can do it like this:

float[,] neighbour = new float[12, 8];

2 Comments

can I use it using pointers then, bcz my whole code is using both of these arrays and I dont wana change it nw..cant I just point that specific [8] index to a pointer array ?? and access it by array[8].pickSomeValue
No, you can't. The array holds float values, not pointers. You'll have to change your existing code.
0

You also could use generics:

List<List<float>> numbers = new List<List<float>>();
numbers.Add(new List<float>());
numbers.Add(new List<float>());
numbers[0].Add(2.3);
numbers[0].Add(5);
numbers[0].Add(8.74);
numbers[1].Add(6.8);
numbers[1].Add(9.87);
float aNumber = numbers[1][0]; //6.8
float anotherNumber = numbers[0][2]; //8.74

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.