Click here to Skip to main content
15,904,638 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.

What I have tried:

#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;
Updated 18-Mar-21 8:15am
BillWoodruff 18-Mar-21 11:06am    
Are you asking how to use "unsafe" code in C# ... or, are you asking how to write a C# equivalent to the C function ?
priyamtheone 18-Mar-21 12:52pm    
@BillWoodruff: I'm not asking to write unsafe code. I'm just asking how to write the C# equivalent to the C function, especially, how do we make use of pointer concepts like &i and *p in C#. For example, &i holds the memory address of the variable i. So how do we get the memory address of a variabe in C#? Similarly, if p = 100, then *p returns the value being held at the memory location of 100. So, how do we do this in C#?
BillWoodruff 18-Mar-21 13:29pm    
see my post below

Consider a simple function:
public void f1(ref int iref)
    iref = iref * iref;

// calling the function:

int ival = 4;
f1(ref ival);  // ival is now #16
In C# we can use the 'ref parameter attribute to tell the compiler to pass the address of the variable rather than its value. In this case Type 'int is a ValueType (stored on the Stack).

To really get a foundation for understanding how Value/Reference Types work in C#, you need to ... as OriginalGriff wisely advises you to ... realize C# is a very different, and equally complex, reality than C: C# manages memory allocation for you using the stack and heap, and has quite complex garbage collection algorithms which are seldom directly modified by the programmer.

Suggested reading:

0 stack and heap: [^]

1 ValueType and Reference Type: [^]

2 use of 'ref, 'in, and 'out parameter attributes: [^]

3 Class and Struct: [^]

4 special case Arrays: [^]

5 special case String: [^], [^], [^]

6 Mutable and Immutable: [^]

7 Boxing and Unboxing: [^]
Share this answer
priyamtheone 18-Mar-21 15:19pm    
Thanks for the tutorial references. They'll definitely be helpful in improving my knowledge base.
Maciej Los 19-Mar-21 3:09am    
Although Maciej is correct, you probably shouldn't be using pointers directly in C# - that's one of the reasons why it is difficult to do: to remind you that this is not a good idea unless you really, really, need to (and in the 14 years I've been using C#, I haven't needed a single pointer yet).

Instead, you need to stop assuming that C (or C++) and C# are the same language, and realize that they are very different languages which share some similar syntax. Trying to write C code in C# will end up with bad code that is hard to use, hard to maintain, and hard to read.
The C# equivalent of that code doesn't need pointers because C# is reference based - and you can consider references as "pointers on steroids"!
private static void f(int p, ref int q)
   q = 2;

private static int i = 0, j = 1;

public static int Main()
   f(i, ref j);
   Console.WriteLine($"{i} {j}");
   return 0;
But if you use more complex objects, you don't need to pass by reference at all.
Share this answer
jsc42 18-Mar-21 13:06pm    
Should 'f(i, ref j);' be 'f(i, out j);'?
OriginalGriff 18-Mar-21 13:33pm    
It could be - but the C original doesn't allow for one-way references, so ref was closer to the actual pointer.

And that's ignoring that you wouldn't write code like that in C# anyway ... :laugh:
OriginalGriff 18-Mar-21 13:35pm    
out would also complicate matters, as you must set a value to out parameters in all code paths or get a compiler error. Wouldn't be a problem in this trivial code, but ...
priyamtheone 18-Mar-21 14:37pm    
OriginalGriff: Thanks for the explanation. After going through it, I'll make two assumptions based on the context of C# language: 1) If we write
int* p = &a;
, the pointer variable will hold the memory address of the int variable a (suppose 1001).

2) If p is currently holding 1001, then writing
return *p;
will return the value being held at the memory location of 1001.

Am I right to make the above two assumptions?
OriginalGriff 18-Mar-21 15:06pm    
Yes and no: if you go assigning random numbers to a pointer, what will actually happen is your app will crash with a "memory access violation" or a "segmentation fault"
This is generally what you get when you forget to assign a value to a pointer before you use it!
Start here: Pointer types - C# Programming Guide | Microsoft Docs[^]

MSDN wrote:

Pointer types (C# Programming Guide)

In an unsafe context, a type may be a pointer type, a value type, or a reference type. A pointer type declaration takes one of the following forms:

type* identifier;
void* identifier; //allowed but not recommended

The type specified before the * in a pointer type is called the referent type. Only an unmanaged type can be a referent type.

Pointer types do not inherit from object and no conversions exist between pointer types and object. Also, boxing and unboxing do not support pointers. However, you can convert between different pointer types and between pointer types and integral types.

When you declare multiple pointers in the same declaration, the asterisk (*) is written together with the underlying type only; it is not used as a prefix to each pointer name. For example:

int* p1, p2, p3;   // Ok
int *p1, *p2, *p3;   // Invalid in C#

Share this answer
priyamtheone 18-Mar-21 14:22pm    
@Maciej Los: Thanks for the MSDN article. It was really helpful in comprehending the topic. I bookmarked it.

Just two things to clear after reading the topic- In C, &a gets the memory address of the int variable a. So, in C#, does writing
int* p = &a;
hold the memory address of a (suppose 1001) in the pointer variable?

Secondly, in C, if p = 1001, then *p returns the value being held at the memory location of 1001. So, if we write
return *p;
in C#, will it return the value being held at the memory location of 1001 the same way in C?
Maciej Los 19-Mar-21 3:26am    
Sorry, i was bit busy yesterday...
I'm not sure how it works in C#, because i've never used pointers in C#, as same as OriginalGriff. As to me, it should work the same way as in C.

The simplest way to find out is to write simple program. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900