I've been reading on these for some time now, and as a long time code maker for video games I have a completely different look on them since back then I was just going by what I was teaching myself. But I'm wanting to know how to assign the pointer to where it goes. Like say I have a pointer named MyPointer and I want it to point to a variable that I've called MyVariable; how do I do this?

public int MyVariable {get; private set;}
public int* MyPointer;
5 Years
Discussion Span
Last Post by lxXTaCoXxl

Hope this helps.

int x = 10;
int* p = &x; // Assign pointer p the memory address of x.

Console.WriteLine((int)p); // Output the memory address of x.

int y = *p; // Assign variable y the value of the variable pointer p points to.

Console.WriteLine(y); // As expected, output is 10 as this is what x's value is.

 * ==============
 * 104851492
 * 10

Also, don't forget to compile with /unsafe and add the unsafe keyword to your main method and you'll be fine. Don't forget to mark the thread as solved if you feel your problem is solved. If not, feel free to ask anything else you want and I'll be happy to answer it for you (as long as I know it, of course since I'm only a hobbyist programmer!). Have a nice day! :P


Okay so how would I make the address of variable x dynamic?

I do understand how to use pointers a little better now so thanks for that much. But I do want a dynamic variable; I've heard that with arrays you declare the array but you don't assign the size of it until it's needed and that makes it dynamic but I don't see how that would work with a simple variable nor do I see how to use a pointer to access it. What is the major importance of pointers in C#?

Edited by lxXTaCoXxl: n/a


I'm sorry my friend but I don't really know. It's been quite a while since I last programmed in C#, let alone use pointers. I have gotten to the point where I know how to use them but can't really tell why I should (namely I'm in pretty much the same position as you are from what I can tell). Sorry again xD


LOL It's cool man. I'm needing some people that know how to program in C# to add to my Skype haha that way it's a little faster than waiting for replies of people who aren't even online. xD


to create a dynamic array

int *ptrInt;
ptrInt=new int[5];

now to access it first element


i hope it helps


I'll give it a try. But is it only an array or is there a way to do it with standard variables?


i don't know how to make the standard variables dynamic since memory allocation for standard variables would be done on compile time..may be this is how you'd do it
suppose you want a dynamic variable 'A'

int *ptrVarA;
ptrVarA=new int[1];

this way,the memory allocation would be done during runtime


Not really as far as I know, but still it's useful to know how in case the need ever does come up.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.