473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to define a pointer to an array?

I can't manage to define a simple pointer to an array.
The following C++ code doesn't work in C# (compiler error: "You can
only take the address of unfixed expression inside of a fixed statement
initializer")

Int32 *pointer;
int []array = new int[100];
pointer = &array[0];

Can anyone please instruct me how to do this properly in C#?
Thanks,
Peter

Nov 17 '05 #1
5 42807
Peter,
Can anyone please instruct me how to do this properly in C#?


If you really have to do it:

unsafe {
fixed (int* pointer = array) {
// Do stuff here
}
}
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2
You can define a pointer to an array... but only within a limited area by
using the fixed statement
(http://msdn.microsoft.com/library/de...vclrffixed.asp) ... in this case you would do it like this:

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

By using the fixed keyword, you are telling the CLR that you want to force
it not to move the data that the pointer is pointing at around in memory
(which is a real risk with managed code).

Be sure to note that the pointer is only usable within with in the statement
(ie between { and }) immediately following the fixed keyword, so you cannot
take this pointer with you... however it is still usable for things like
P/Invokes and some pointer arithmetic.

Brendan
"Peter Demeyer" wrote:
I can't manage to define a simple pointer to an array.
The following C++ code doesn't work in C# (compiler error: "You can
only take the address of unfixed expression inside of a fixed statement
initializer")

Int32 *pointer;
int []array = new int[100];
pointer = &array[0];

Can anyone please instruct me how to do this properly in C#?
Thanks,
Peter

Nov 17 '05 #3
Ron
Here's how you do it:
http://msdn.microsoft.com/library/de...vclrffixed.asp

"Peter Demeyer" wrote:
I can't manage to define a simple pointer to an array.
The following C++ code doesn't work in C# (compiler error: "You can
only take the address of unfixed expression inside of a fixed statement
initializer")

Int32 *pointer;
int []array = new int[100];
pointer = &array[0];

Can anyone please instruct me how to do this properly in C#?
Thanks,
Peter

Nov 17 '05 #4
Peter Demeyer <de***********@gmail.com> wrote:
I can't manage to define a simple pointer to an array.
The following C++ code doesn't work in C# (compiler error: "You can
only take the address of unfixed expression inside of a fixed statement
initializer")

Int32 *pointer;
int []array = new int[100];
pointer = &array[0];

Can anyone please instruct me how to do this properly in C#?


Others have given you the answer of how - could I ask *why* you need to
do this? It could well be that there's an answer to the problem you're
trying to tackle that doesn't require pointers.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
Thank you all very much!
I managed to do what I wanted with your help.
Peter

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bryan Parkoff | last post by:
I want to allocate pointer array into memory so pointer array contains ten pointers. It would be 4 bytes per pointer to be total 40 bytes. Looks like below for example. unsigned char* A = new...
5
by: Brett | last post by:
Sorry for the rookie question.... If I have a pointer array (for example): char *colors = { "blue", "green" }; and I want to add yellow to this array later in my code....how would I do
7
by: Roman Mashak | last post by:
Hello, All! I wonder is it possible to define an array containing strings, not single characters? What I want is array 'table' that will have N elements, and every element is a strings tailoring...
5
by: Stuart Norris | last post by:
Dear Readers, I am attempting to define an array of IPAddress-es in C#. I wish to have a array of address so I can try in order to connect to them in a loop to handle unavailable hosts. ...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
5
Nosnibor
by: Nosnibor | last post by:
Need some direction as how to implement a pointer array which will mimic a 2D array. Any suggestions? This is where I am. Do not work, but i am working on it. Please send suggestions. I think my...
8
by: Susan Bricker | last post by:
Hi. I would like to create an array and use the data to populate some new records when the user clicks on a button (that's the short description). If I were to code the array in C (I know ......
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
13
by: pereges | last post by:
Hi, can some one please tell me why this program is not able to function properly. I have a array a and i am trying to create a pointer array b which points to elements less than 40 in a. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.