Returning array from a function (139 Views)
Reply
Advisor
msbinu
Posts: 36
Registered: ‎04-26-2006
Message 1 of 7 (139 Views)

Returning array from a function

Hi

I have an array unsigned char * abc[16]

If I print the contents like below,

for (i = 0; i < 16; i++)
printf ("%02x", mdContext->digest[i]);

I will get the following output

21232f297a57a5a743894a0e4a801fc3

I want to pass the same values to the calling funtion from there i wants to write the same thing to a socket .

I was facing with returing array form tht function .

Hence I did some thing like the following

RWCString abc;
for (int i = 0; i < 16; i++)
abc.append( mdContext.digest[i]);
cout <<" binu " << abc < return abc.data();

But here I m not getting the correct data ... I m getting some thing like
©ý¤¼Þ²)8ül&¿G

can any one help me to retrive proper data

Regards
BINu
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 2 of 7 (139 Views)

Re: Returning array from a function

Returning a character array is really no different than returning any other kind of array. Note that you are actually returning the address of an array. Now let's see what's really wrong.

char *bad_array()
{
char abc[16];

(void) sprintf("abcdefghijklmno");
return(abc);
} /* bad_array */


char *good_array()
{
static char abc[16];

(void) sprintf("ABCDEFGHIJKLMNO");
return(abc);
} /* bad_array */

int main()
{
(void) printf("Bad (maybe) : %s Good: %s\n",bad_array(),good_array());
return(0);
}

The problem with bad_array is that while the address it returns is valid, it points to an auto storage class variable that has now gone out of scope and if it works, it works by accident. Using a static variable within the function means that the data is not allocated from the stack and the contents remain valid.

If it ain't broke, I can fix that.
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 3 of 7 (139 Views)

Re: Returning array from a function

and as a test of further understanding, try this:

char *Good_array(int i)
{
static char abc[16];

(void) sprintf("Good %d",i);
return(abc);
} /* Good_array */

int main()
{
(void) printf("%s %s %s %s\n",Good_array(1),Good_array(2),Good_array(3),Good_array(4));
return(0);
}

Are the results what you expected?
If it ain't broke, I can fix that.
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 4 of 7 (139 Views)

Re: Returning array from a function

Ooops, I messed up the sprintf. I left out the 1st parameter.

should be:

char *bad_array()
{
char abc[16];

(void) sprintf(abc,"abcdefghijklmno");
return(abc);
} /* bad_array */


char *good_array()
{
static char abc[16];

(void) sprintf(abc,"ABCDEFGHIJKLMNO");
return(abc);
} /* good_array */

int main()
{
(void) printf("Bad (maybe) : %s Good: %s\n",bad_array(),good_array());
return(0);
}

If it ain't broke, I can fix that.
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 5 of 7 (139 Views)

Re: Returning array from a function

and similarly,

should be:

char *Good_array(int i)
{
static char abc[16];

(void) sprintf(abc,"Good %d",i);
return(abc);
} /* Good_array */

int main()
{
(void) printf("%s %s %s %s\n",Good_array(1),Good_array(2),Good_array(3),Good_array(4));
return(0);
}
If it ain't broke, I can fix that.
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,973
Registered: ‎03-06-2006
Message 6 of 7 (139 Views)

Re: Returning array from a function

As Clay says, you can use a static array to make sure the contents are still valid. Unfortunately using a static array isn't thread nor recursion safe.

If you really really want to return an array by value, you can wrap it in a struct. Or you could use Pascal. ;-)

struct return_array { char arr[16]; }
struct return_array ret_array(int i) {
struct return_array abc;
(void)sprintf(abc.arr,"Good %d",i);
return abc;
}
Please use plain text.
Advisor
msbinu
Posts: 36
Registered: ‎04-26-2006
Message 7 of 7 (139 Views)

Re: Returning array from a function

Thanks all
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation