Question What is the counterpart of this C pointer programme in C#?

priyamtheone

Member
Joined
Sep 18, 2011
Messages
11
Programming Experience
Beginner
I'd like to know how the following programme on pointer written in C can be written in C#, especially how to use the keywords in the C programme like &i and *p in C#. Please clarify.

C#:
#include <stdio.h>

void f(int *p, int *q)
{
    p = q;
    *p = 2;
}

int i = 0, j = 1;

int main()
{
    f(&i, &j);
    printf("%d %d \n", i, j);
    return 0;
}
 
In C#, everything are references except for value types. References are simply pointers. In general, C# does not want you playing with pointers so that you can write "safe" code. For the places where you deal with pointers, you need to mark the code as "unsafe", because the compiler has no way of verifying that you are not accidentally doing something unsafe with the pointers.

int are a value type. The way to change the value from within a method is to use the ref keyword in the parameter list. But using ref keyword won't let you change what you are pointing to (e.g. line 5). It will only let you change the value. Changing the value is merely an assignment. There is no need to dereference the pointer. So line 6 should be simply q = 2; in C#.
 
This site is not a code conversion service. As I replied to you elsewhere, it's for you to understand what functionality that C code provides and then make you best attempt to implement the same functionality in C#. You should be thinking in terms of functionality, not code. We can help if you encounter an issue when writing the C# implementation but if you're not even going to try then we're not likely to either.

You also need to decide what language you are using first. You keep asking for both C# and VB code and you're obviously not going to use both, so if people take the time to answer you in both languages then you're wasting someone's time. Not what you should be doing to strangers volunteering their time to help you.
 
This site is not a code conversion service. As I replied to you elsewhere, it's for you to understand what functionality that C code provides and then make you best attempt to implement the same functionality in C#. You should be thinking in terms of functionality, not code. We can help if you encounter an issue when writing the C# implementation but if you're not even going to try then we're not likely to either.

You also need to decide what language you are using first. You keep asking for both C# and VB code and you're obviously not going to use both, so if people take the time to answer you in both languages then you're wasting someone's time. Not what you should be doing to strangers volunteering their time to help you.
I thought that was a different website where you replied me. I didn't know two portals direct to the same website. They even look different. Anyway, as I already clarified you, I'm not seeking code conversion. Rather, I'd like to know how the concepts of address-of operator and de-reference operator are implemented in C# as I'm having a bit of a difficulty understanding them. And of course I'll try to implement the code in C#. Or else, why do you think I'm scouring through references and seeking information on the topic? But before that, I need to get the fundamentals clear on how to go about it in C#, as I've never worked on address-of and de-reference operators in the language, something that OptionBase1 helped me with in the other site. That was the kind of help I was looking for.

I also explained you, a C# reference will suffice. No need for the VB code. That was a redundant post.
 
I thought that was a different website where you replied me. I didn't know two portals direct to the same website. They even look different.
They are different websites. A lot of us volunteers like to visit different forums.
 
Rather, I'd like to know how the concepts of address-of operator and de-reference operator are implemented in C# as I'm having a bit of a difficulty understanding them.
The concepts and and operators work exactly the same in C# as they do in C, assuming that you are writing unsafe code, and you are dealing with value types. See the code I posted in DreamInCode.

With reference types, you'll need to pin the object before you can get a pointer to. (See fixed keyword and/or GCHandle.Alloc().)
 
Back
Top Bottom