-
March 18th, 2021, 10:24 AM
#1
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;
}
-
March 18th, 2021, 11:58 PM
#2
Re: What is the counterpart of this C pointer programme in C#?
-
March 19th, 2021, 09:52 AM
#3
Re: What is the counterpart of this C pointer programme in C#?
 Originally Posted by salem_c
Posting a question in multiple forums enhances the scope of getting answers from more repliers, especially those who are not on other forums.
-
March 19th, 2021, 01:56 PM
#4
Re: What is the counterpart of this C pointer programme in C#?
 Originally Posted by priyamtheone
Please clarify.
One of the main goals of both Java and its follow-up C# is to be resilient to programmer errors. For this reason, you cannot do things like &i and *p. Explicit references and pointers were deemed dangerous and are not part of those languages. C++ takes another approach. You can still do it the C way, but you can also opt for a higher abstraction level to write safer code. In C++, you make this choice, not the language.
Learn about classes and objects in C#, and you will understand how to rewrite your example program.
Last edited by wolle; March 20th, 2021 at 04:52 AM.
-
March 21st, 2021, 05:02 AM
#5
Re: What is the counterpart of this C pointer programme in C#?
> Posting a question in multiple forums enhances the scope of getting answers from more repliers,
Or wastes everyone's time with simple questions that are trivially answered.
My reply wasn't aimed at you, it was for the regulars to consider whether it would be worth investing any of their VALUABLE time in answering your question, by quickly checking whether it's already been answered elsewhere.
This on the other hand is for YOU.
http://www.catb.org/esr/faqs/smart-questions.html#forum
http://www.catb.org/esr/faqs/smart-q...ns.html#urgent
So while you're busy abusing a free and finite resource, if multiple people answer your question in the same way on multiple sites, by definition of a finite resource (our time), someone else doesn't get an answer.
-
March 22nd, 2021, 10:51 AM
#6
Re: What is the counterpart of this C pointer programme in C#?
 Originally Posted by salem_c
> Posting a question in multiple forums enhances the scope of getting answers from more repliers,
Or wastes everyone's time with simple questions that are trivially answered.
So while you're busy abusing a free and finite resource, if multiple people answer your question in the same way on multiple sites, by definition of a finite resource (our time), someone else doesn't get an answer.
Something simple and trivial for you may be something essential and important for someone else, especially someone who's trying to get a fundamental knowledge on the topic. You can't move on to the complexities without getting your basics right.
When a resource is free for my access, I'm free to utilise it in my way too. Using a resource to gain insight on a subject, no matter how rudimentary it is, can't count as abuse.
-
March 22nd, 2021, 11:00 AM
#7
Re: What is the counterpart of this C pointer programme in C#?
 Originally Posted by wolle
One of the main goals of both Java and its follow-up C# is to be resilient to programmer errors. For this reason, you cannot do things like &i and *p. Explicit references and pointers were deemed dangerous and are not part of those languages.
While it's true that references and pointers are deemed dangerous and not part of managed coding in C#, more reading on the topic reveals that we can still work with concepts like address-of and indirection operators. It's only that they are shifted to the unsafe category coding. They still retained the control on programmers to work with references and pointers in certain exceptional situations using extreme caution. Here's a reference to a tutorial I found: https://docs.microsoft.com/en-us/dot.../pointer-types
-
March 23rd, 2021, 02:03 PM
#8
Re: What is the counterpart of this C pointer programme in C#?
 Originally Posted by priyamtheone
work with references and pointers in certain exceptional situations using extreme caution.
In exceptional situations, there is yet another option. You can mix C# and native C++ using C++/CLI as a bridge. At least in theory. I haven't done it, so I cannot vouch for it.
Last edited by wolle; March 23rd, 2021 at 11:40 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|