|
-
Mar 18th, 2021, 10:05 AM
#1
Thread Starter
Junior Member
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;
}
-
Mar 18th, 2021, 10:41 AM
#2
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.
-
Mar 18th, 2021, 10:49 AM
#3
Re: What is the counterpart of this C pointer programme in VB.Net?
Hi,
You may find this useful.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Mar 18th, 2021, 11:07 AM
#4
Re: What is the counterpart of this C pointer programme in VB.Net?
 Originally Posted by Poppa Mintin
Hi,
You may find this useful.
Poppa
 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.
My usual boring signature: Nothing
 
-
Mar 18th, 2021, 02:35 PM
#5
Thread Starter
Junior Member
Re: What is the counterpart of this C pointer programme in VB.Net?
 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.
Last edited by priyamtheone; Mar 18th, 2021 at 03:02 PM.
-
Mar 18th, 2021, 02:41 PM
#6
Thread Starter
Junior Member
Re: What is the counterpart of this C pointer programme in VB.Net?
 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?
-
Mar 18th, 2021, 02:53 PM
#7
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.
Last edited by OptionBase1; Mar 18th, 2021 at 02:59 PM.
-
Mar 18th, 2021, 03:07 PM
#8
Thread Starter
Junior Member
Re: What is the counterpart of this C pointer programme in VB.Net?
 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!
-
Mar 18th, 2021, 04:08 PM
#9
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.
My usual boring signature: Nothing
 
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|