Address in C#

G

Guest

Please answer my following questions.
(1). How to print address of any variable in C#. for example
int x; int [] y=new int[4];
how to printf address of x and address of first element of array y.

(2)In C we could print values of any decimal number in Hex or Octal by
simply writing %h or %O. how can we do it in C#, windows programming.

(3)what is difference between safe and unsafe region.
(4)What is difference between fixed and non fixed.
(5)Can any body point me to ebook on net which cover pointers in C# in
details
 
J

Jon Skeet [C# MVP]

javaid iqbal said:
Please answer my following questions.
(1). How to print address of any variable in C#. for example
int x; int [] y=new int[4];
how to printf address of x and address of first element of array y.

You can't in safe code; in unsafe code you can take a pointer in
roughly the same way you do in C:

int x = 10;
int* y = &x;
(2)In C we could print values of any decimal number in Hex or Octal by
simply writing %h or %O. how can we do it in C#, windows programming.

Look up "standard numeric format strings" in MSDN.
(3)what is difference between safe and unsafe region.

If you look up "unsafe code" in MSDN, there's a fairly in-depth
tutorial
(4)What is difference between fixed and non fixed.

That's covered in the above. Basically when an object is fixed, it
can't be moved by the garbage collector.
(5)Can any body point me to ebook on net which cover pointers in C# in
details

A search for unsafe and C# will find you quite a few articles. Why do
you think you need pointers, out of interest?
 
G

Guest

Thanks Jon for quick reply.

If i do following in unsafe code.
int s;
label1.Text = &s.ToString();

I get following errors. how to fix it

D:\mysource\C Sharp\WindowsApplicationOCX\Form1.cs(236): Cannot implicitly
convert type 'string*' to 'string'
D:\mysource\C Sharp\WindowsApplicationOCX\Form1.cs(236): Cannot take the
address of the given expression
D:\mysource\C Sharp\WindowsApplicationOCX\Form1.cs(236): Cannot take the
address or size of a variable of a managed type ('string')
D:\mysource\C Sharp\WindowsApplicationOCX\Form1.cs(236): You can only take
the address of unfixed expression inside of a fixed statement initializer
 
M

Morten Wennevik

Javaid,

label1.Text is a string property dealing with string references not the adress

label1.Text = n.ToString();
 
J

Jon Skeet [C# MVP]

javaid iqbal said:
Sir my question is how to print the address of integer x.

Then you certainly don't want to be using string pointers. You can do:

using System;

class Test
{
unsafe static void Main()
{
int x = 10;
int* y = &x;
Console.WriteLine(new IntPtr(y));
}
}

I would question how useful this is though.
 
C

Christof Nordiek

Hi javaid

The meaning of &s.ToString() is: Get the string-representation of s and get
the address of it.
Try: (&s).ToString()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top