Results 1 to 6 of 6

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

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

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

    Quote Originally Posted by jmcilhinney View Post
    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
    Code:
    int* p = &a;
    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
    Code:
    return *p;
    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.

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

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


  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

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

    Quote Originally Posted by Peter Swinkels View Post
    That's exactly what I was looking for. Thanks for sharing.

  6. #6
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

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

    You’re welcome.

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