What is the counterpart of this C pointer programme in C#?
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.
Code:
#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;
}
Re: What is the counterpart of this C pointer programme in C#?
You've posted this question in both the VB.NET and C# forums. You don't need to write the same app in both languages so how about you make up your mind which you want and save us wasting our time doing twice the work? Instead of expecting us to translate code for you, how about you understand the functionality implemented in the C code and then work out how to implement the same functionality in your chosen language? C# supports pointers while VB.NET does, but you almost certainly shouldn't be using pointers in C# for something this trivial. They should only be used in specific situations where they make a significant difference to performance.
Re: What is the counterpart of this C pointer programme in C#?
Quote:
Originally Posted by
jmcilhinney
You've posted this question in both the VB.NET and C# forums. You don't need to write the same app in both languages so how about you make up your mind which you want and save us wasting our time doing twice the work? Instead of expecting us to translate code for you, how about you understand the functionality implemented in the C code and then work out how to implement the same functionality in your chosen language? C# supports pointers while VB.NET does, but you almost certainly shouldn't be using pointers in C# for something this trivial. They should only be used in specific situations where they make a significant difference to performance.
I'm not expecting someone to blindly translate the code for me. As C# language is different from C, I'd like to know how pointer concepts are handled in a given situation like the code mentioned in my above post. As I mentioned in my original post, I would specifically like to know how &i and *p concepts are handled in C#. A trivial code helps in comprehending the basic concepts before heading to the more complex ones.
For example, In C, &a gets the memory address of the int variable a. So, in C#, does writing hold the memory address of a (suppose 1001) in the pointer variable?
Secondly, in C, if p is currently holding 1001, then *p returns the value being held at the memory location of 1001. So, in C#, if we write will it return the value being held at the memory location of 1001 the same way in C?
I want exactly these two concepts to be clear in context of the C# language. Are they possible this way? And, would I be right if I do them this way in C#?
Hope I'm more clear this time.
Re: What is the counterpart of this C pointer programme in C#?
Re: What is the counterpart of this C pointer programme in C#?
Quote:
Originally Posted by
Peter Swinkels
That's exactly what I was looking for. Thanks for sharing.
Re: What is the counterpart of this C pointer programme in C#?