CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2008
    Posts
    90

    Question 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;
    }

  2. #2
    Join Date
    Nov 2018
    Posts
    120

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


  3. #3
    Join Date
    Sep 2008
    Posts
    90

    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, especially those who are not on other forums.

  4. #4
    Join Date
    Feb 2017
    Posts
    677

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

    Quote Originally Posted by priyamtheone View Post
    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.

  5. #5
    Join Date
    Nov 2018
    Posts
    120

    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.

  6. #6
    Join Date
    Sep 2008
    Posts
    90

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

    Quote Originally Posted by salem_c View Post
    > 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.

  7. #7
    Join Date
    Sep 2008
    Posts
    90

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

    Quote Originally Posted by wolle View Post
    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

  8. #8
    Join Date
    Feb 2017
    Posts
    677

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

    Quote Originally Posted by priyamtheone View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured