What is the counterpart of this C pointer programme in VB.Net?
I'd like to know how the following programme on pointer written in C can be written in VB.Net, especially how to use the keywords in the C programme like &i and *p in VB.Net. 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;
}
Re: What is the counterpart of this C pointer programme in VB.Net?
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.
Re: What is the counterpart of this C pointer programme in VB.Net?
Hi,
You may find this useful.
Poppa
Re: What is the counterpart of this C pointer programme in VB.Net?
Quote:
Originally Posted by
Poppa Mintin
Hi,
You may find
this useful.
Poppa
:lol::lol: I didn't see the link at first, and thought you had either forgotten something, or were making some kind of ironic comment that I didn't understand.
Re: What is the counterpart of this C pointer programme in VB.Net?
Quote:
Originally Posted by
jmcilhinney
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.
The question got posted in VB.Net forum redundantly as that's when the router got to be restarted. I thought it didn't get posted here. However, we've got a significant answer from OptionBase1 here. So let this post remain for future reference.
Re: What is the counterpart of this C pointer programme in VB.Net?
Quote:
Originally Posted by
Poppa Mintin
Hi,
You may find
this useful.
Poppa
May be I misunderstood, but I don't see any option in that site to convert code from "C" to C#. Am I missing something?
Re: What is the counterpart of this C pointer programme in VB.Net?
From the book Inside C#, ISBN 0735616485:
Code:
Operator Description
& The address-of operator returns a pointer that represents the memory address of the variable.
* The dereference operator is used to denote the value pointed at by the pointer.
-> The dereferencing and member access operator is used for member access and pointer dereferencing.
Code:
// Compile this application with the /unsafe option.
using System;
class Unsafe1App
{
public static unsafe void GetValues(int* x, int* y)
{
*x = 6;
*y = 42;
}
public static unsafe void Main()
{
int a = 1;
int b = 2;
Console.WriteLine("Before GetValues() : a = {0}, b = {1}", a, b);
GetValues(&a, &b);
Console.WriteLine("After GetValues() : a = {0}, b = {1}", a, b);
}
}
Edited to add:
The book this came from was published for C# from VS.NET 2002, which was the first version of C#. That's also the only version of C# I've ever used, so it is entirely possible that things have changed since then, such as the whole "unsafe" stuff mentioned above.
Good luck.
Re: What is the counterpart of this C pointer programme in VB.Net?
Quote:
Originally Posted by
OptionBase1
From the book Inside C#, ISBN 0735616485:
Code:
Operator Description
& The address-of operator returns a pointer that represents the memory address of the variable.
* The dereference operator is used to denote the value pointed at by the pointer.
-> The dereferencing and member access operator is used for member access and pointer dereferencing.
Code:
// Compile this application with the /unsafe option.
using System;
class Unsafe1App
{
public static unsafe void GetValues(int* x, int* y)
{
*x = 6;
*y = 42;
}
public static unsafe void Main()
{
int a = 1;
int b = 2;
Console.WriteLine("Before GetValues() : a = {0}, b = {1}", a, b);
GetValues(&a, &b);
Console.WriteLine("After GetValues() : a = {0}, b = {1}", a, b);
}
}
Edited to add:
The book this came from was published for C# from VS.NET 2002, which was the first version of C#. That's also the only version of C# I've ever used, so it is entirely possible that things have changed since then, such as the whole "unsafe" stuff mentioned above.
Good luck.
Very precise and relevant answer. I was looking for a reference exactly like this. This answer set the ball rolling for me. Now I can take the pointer concepts in C# further. Thank you very much!
Re: What is the counterpart of this C pointer programme in VB.Net?
Well, you're going to HAVE to keep going in C#, because unsafe pointers have never been a part of VB.