Click here to Skip to main content
15,042,088 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
how can i pass address of a structure to a variable?? is it possible in c#??
byte b=&array[0]; is it possible??
If sounds like "C thinking". You need to go beyond that, to understand things. Why address? Do you understand that managed pointers (references) are not fixed (there is a procedure of pinning, used in really rare cases)? they are managed.

Please see my comment to the question. You intention is not explained, so I can only guess that this is the sign of "C thinking". (Not just C, but I hope you understand what I mean.)

Without understanding of your problems, it's hard to advise anything certain, but I'll try to make some hints. First thing you need to understand is that the address of the instance of some structure is needed not for itself, but for accessing the structure. Second thing: you need to know how reference types work (heap is always involved), in contrast to value types which only appear on heap be being used in some reference type. The structures are value types. Therefore, they are copied memberwise when assigned. So, perhaps another thing to understand is by-value and by-reference parameter passing. Compare:
internal struct SomeValueType {
    // usually fields are private,
    // but, for example:
    internal int SomeMember; 
    internal SomeValueType(int someMember) { SomeMember = someMember; /*...*/ }
    // ...

// ...

void ByValue(SomeValueType someParameter) {
    // this is a copy of a parameter put on stack:
    ++someParameter.SomeMember; // will not affect the actual parameter 
    // ...

void ByRef(ref SomeValueType someParameter) { // reference is passed
    ++someParameter.SomeMember; // will not affect the actual parameter 
    // ...

void ByRefOut(out SomeValueType someParameter) {
    someParameter = new SomeValueType(13); // it is required to initialize the parameter


SomeValueType myInstance = new SomeValueType(3);
ByValue(myInstance); // now myInstance.SomeMember is 3, not 4!
ByRef(myInstance); // now myInstance.SomeMember is 4
ByRefOut(myInstance); // referential identity of myInstance 
// and previous myInstance object is broken:
// now this is a brand-new object

There are a lot more situations where you need to understand well what happens to the reference to some object on heap, stack and static memory area; you need to understand static and instance members, and a lot more. And you need to understand that managed pointers are not directly bound to physical (and not even to virtual) addresses core OS works with: they can be relocated in consistent manner, so in each point of the code the references keep pointing to the same objects, but the objects themselves can move around in memory space. This is one of the reason for unsafe and pinning (read about it).

I hope it can give you some insight.

Well...not like that: that wouldn't work in any language, since a byte variable can't hold the address of anything - they are only 8 bits, and addresses these days are at least 32.

And there is a problem here in that an array isn't a structure at all, it's always a class (and so a reference type), even if the datatype it is an array of are value types.

You can get the address of things in C#, but you need to use unsafe code and pointers. which is:
A) Deliberatley difficult to do
B) Dangerous - which is why it's called unsafe code.

Under normal circumstances you don't need pointers or addresses in C#: the CLR and references take care of all that for you.

What are you trying to do that you think you need this?

"i need array[0]'s address"

It doesn't really have one. does...but it moves...
The problem is that an array is a reference type, which means the whole array is located on the managed heap - which means that the Garbage Collector (which you have no control of, or idea of when it runs) is at liberty to move it at any time.
So if you do manage to get the actual "physical memory" address of the first element in the array, it could be invalid by the time you actually use it - even if that is the next instruction in your code. Now, there are ways round that, but it's unusual, and somewhat dangerous.

You can get a reference of the first element in the array - if it's a reference type - very easily:
string[] array = new string[10];
for (int i = 0; i < 10; i++)
   array[i] = string.Format("Element {0}", i);
string s = array[0];

Why are you trying to get the address of anything? what are you planning on doing with it when you have it? Because I can't help thinking there is something missing here...
Member 10994712 13-Aug-14 1:04am
i need array[0]'s address
OriginalGriff 13-Aug-14 2:16am
Answer updated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900