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.

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

Recommended Answers

This is presumably all about passing params as references rather than values (the default in C#)
To pass a parameter as a reference use the ref keyword.
See https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/passing-parameters

Jump to Post

All 2 Replies

The function f appears to put the value 2 into the second argument.
You can test that out as need be. No conversion appears to be required as I would toss function f and set j to 2. Done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.