By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,814 Members | 1,678 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,814 IT Pros & Developers. It's quick & easy.

Array pointers in C#

P: 1
Hi guys

Im attempting to write conways Game of life and need to use a pointer and 2 arrays. The reason im using a pointer is hopefully to speed up the code rather than copying the array > processing it and copying it back.

I found this code in an old post on the forum but having never used pointers before im a little lost with what to do.

int []array = new int[100];
fixed(int* pointer = &array[0])
{
//use the pointer
}

my code will use two arrays, grid1 and grid2

thus I suspect my code should look like:

Cell [,]grid1 = new Cell[10,10];
Cell [,]grid2 = new Cell[10,10];
fixed(int* pointer = &grid1[0,0])
{
//use the pointer
}

So firstly is this line .....fixed(int* pointer = &Cell[0,0]) just saying basically the pointer called pointer refers to the array grid1[0,0] ?

And secondly how would I manipulate data in the array being pointed to?

Do i use pointer[0,0] etc as if it were a normal array?

Sorry for such a long post but I've never had to use pointers before and im finding a few posts on the topic a little confusing.

Anyways any help would be greatly apreciated, thanks
Oct 7 '09 #1
Share this Question
Share on Google+
1 Reply


GaryTexmo
Expert 100+
P: 1,501
I'm reasonably sure anything except simple value types are passed by reference already in C#. Simple value types are things like int, bool, double, etc...

So I'm fairly sure that your arrays are already being passed by reference. Still, you can explicitly state this by adding the ref keyword to your parameter declaration..

Expand|Select|Wrap|Line Numbers
  1. public void ManipulateArray(ref int[] intArray)
  2. {
  3.   ...
  4. }
As for how you manipulate it, treat it like any other array.
Oct 7 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.