digitalmars.D - what's the equivilent of C pointers you pass to functions?
- clayasaurus <clayasaurus_member pathlink.com> May 14 2004
- Daniel Horn <hellcatv hotmail.com> May 14 2004
- clayasaurus <clayasaurus_member pathlink.com> May 14 2004
- "Vathix" <vathixSpamFix dprogramming.com> May 14 2004
hello, I'm in a bit of a rut here with D. What I want to do is pass a pointer to
a function and have that function change it's value. For example, in C you can
use
function(int *bob)
{
bob = 6;
}
main()
{
int bob = 7;
function(&bob);
// bob now equals 6
}
I've tried this in D, but D doesn't have the & keyword and D segfaults when I
try to change the value of a declared pointer. Like if i do
int *bob;
*bob = 7;
i get a segmentation fault probably due to D's garbage collector or something.
anyways, all I'm asking is what is the D equivilent of pointers that you can
pass to functions and change their value? thanks.
May 14 2004
void function (inout int bob) ... then you don't need to dereference. clayasaurus wrote:hello, I'm in a bit of a rut here with D. What I want to do is pass a pointer to a function and have that function change it's value. For example, in C you can use function(int *bob) { bob = 6; } main() { int bob = 7; function(&bob); // bob now equals 6 } I've tried this in D, but D doesn't have the & keyword and D segfaults when I try to change the value of a declared pointer. Like if i do int *bob; *bob = 7; i get a segmentation fault probably due to D's garbage collector or something. anyways, all I'm asking is what is the D equivilent of pointers that you can pass to functions and change their value? thanks.
May 14 2004
In article <c836e1$19cn$1 digitaldaemon.com>, Daniel Horn says...void function (inout int bob) ... then you don't need to dereference.
wow, that's pretty sweet & simple. D is awesome.
May 14 2004
"clayasaurus" <clayasaurus_member pathlink.com> wrote in message news:c835qs$18ji$1 digitaldaemon.com...hello, I'm in a bit of a rut here with D. What I want to do is pass a
a function and have that function change it's value. For example, in C you
use function(int *bob) { bob = 6; } main() { int bob = 7; function(&bob); // bob now equals 6 } I've tried this in D, but D doesn't have the & keyword and D segfaults
try to change the value of a declared pointer. Like if i do
D does have C's & operator. You must specify a return type in functions, and the word 'function' is a keyword. This works: void func(int *bob) { *bob = 6; } int main() { int bob = 7; func(&bob); // bob now equals 6 return 0; }int *bob; *bob = 7; i get a segmentation fault probably due to D's garbage collector or
anyways, all I'm asking is what is the D equivilent of pointers that you
pass to functions and change their value? thanks.
This will most likely not work in C either; and it's better off not working because if it does work, it'll just end up corrupting something. bob must point somewhere: int foo; int *bob = &foo; // or use 'new', etc. *bob = 7; -- Christopher E. Miller
May 14 2004









clayasaurus <clayasaurus_member pathlink.com> 