www.digitalmars.com         C & C++   DMDScript  

D - Stupid out param question

reply Taylor <Taylor_member pathlink.com> writes:
I'm trying to write to a dynamic string array that's a parameter to a function
and having no luck. I'm pretty sure that I am missing something, but, after
trying a few things, have no clue as to what I am doing wrong. Here's the test
code that I have written to test a few scenarios:

void testFunc(char[] test)
{
printf("test.length = %d\n", test.length);
test = "12345";
printf("test.length = %d\n", test.length);
}

void testFunc2(char* test)
{
test = new char[5];
test = "12345";
}

void testFunc3(char** test)
{
*test = new char [5];
*test = "12345";
}

void testFunc4(out char[] test)
{
test = "12345";
}

char[] testFunc5(char[] test)
{
test = "12345";
return test;
}

void main(char[][] arg)
{
char[] a;
printf("a.length = %d\n", a.length);
testFunc(a);

printf("a: %.s\n", a);
printf("a.length = %d\n", a.length);

char* b;
testFunc2(b);
printf("b: %.s\n", b);

char* c;
testFunc3(&c);
printf("c: %.s\n", c);

char[] d;
testFunc4(d);
printf("d: %.s\n", d);

char[] d = testFunc5(d);
printf("d: %.s\n", d);
}

None of the five scenarios print anything out (actually the result of testFunc4
and testFunc5 are Access Violations (these were expected, but I just added them
to see if there was something I was missing...)) To me, the most obvious test
that should have worked was the first one.

Any help would be appreciated...

Thanks,
Taylor Boon
Aug 08 2003
next sibling parent "Mike Wynn" <mike.wynn l8night.co.uk> writes:
try "%.*s" if your pass a char[] and just "%s" for a char *

did this compile ?
char[] d = testFunc5(d); should have given you an error (d already defined
in main)


"Taylor" <Taylor_member pathlink.com> wrote in message
news:bh0srb$8nb$1 digitaldaemon.com...
 I'm trying to write to a dynamic string array that's a parameter to a
function
 and having no luck. I'm pretty sure that I am missing something, but,
after
 trying a few things, have no clue as to what I am doing wrong. Here's the
test
 code that I have written to test a few scenarios:

 void testFunc(char[] test)
 {
 printf("test.length = %d\n", test.length);
 test = "12345";
 printf("test.length = %d\n", test.length);
 }

 void testFunc2(char* test)
 {
 test = new char[5];
 test = "12345";
 }

 void testFunc3(char** test)
 {
 *test = new char [5];
 *test = "12345";
 }

 void testFunc4(out char[] test)
 {
 test = "12345";
 }

 char[] testFunc5(char[] test)
 {
 test = "12345";
 return test;
 }

 void main(char[][] arg)
 {
 char[] a;
 printf("a.length = %d\n", a.length);
 testFunc(a);

 printf("a: %.s\n", a);
 printf("a.length = %d\n", a.length);

 char* b;
 testFunc2(b);
 printf("b: %.s\n", b);

 char* c;
 testFunc3(&c);
 printf("c: %.s\n", c);

 char[] d;
 testFunc4(d);
 printf("d: %.s\n", d);

 char[] d = testFunc5(d);
 printf("d: %.s\n", d);
 }

 None of the five scenarios print anything out (actually the result of
testFunc4
 and testFunc5 are Access Violations (these were expected, but I just added
them
 to see if there was something I was missing...)) To me, the most obvious
test
 that should have worked was the first one.

 Any help would be appreciated...

 Thanks,
 Taylor Boon
Aug 08 2003
prev sibling parent "Mike Wynn" <mike.wynn l8night.co.uk> writes:
// ----- try this it compiles and works without error.
import c.stdio;

void testFunc(char[] test)
{
printf("test.length = %d\n", test.length);
test = "12345";
printf("test.length = %d\n", test.length);
}

void testFunc2(char* test)
{
test = new char[5];
test = "12345";
}

void testFunc3(char** test)
{
*test = new char [5];
*test = "12345";
}

void testFunc4(out char[] test)
{
test = "12345";
}

char[] testFunc5(char[] test)
{
test = "12345";
return test;
}

void main(char[][] arg)
{
char[] a;
printf("a.length = %d\n", a.length);
testFunc(a);

printf("a: %.s\n", a);
printf("a.length = %d\n", a.length);

char* b;
testFunc2(b);
printf("b: %s\n", b);

char* c;
testFunc3(&c);
printf("c: %s\n", c);

char[] d;
testFunc4(d);
printf("d: %.*s\n", d);

d = testFunc5(d);
printf("d: %.*s\n", d);
}


/* output is as your would expect!
a.length = 0
test.length = 0
test.length = 5
a:
a.length = 0
b: (null)
c: 12345
d: 12345
d: 12345
*/

"Taylor" <Taylor_member pathlink.com> wrote in message
news:bh0srb$8nb$1 digitaldaemon.com...
 I'm trying to write to a dynamic string array that's a parameter to a
function
 and having no luck. I'm pretty sure that I am missing something, but,
after
 trying a few things, have no clue as to what I am doing wrong. Here's the
test
 code that I have written to test a few scenarios:
Aug 08 2003