Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Returning array from a function

Highlighted
msbinu
Advisor

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
6 REPLIES
A. Clay Stephenson
Acclaimed Contributor

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.
A. Clay Stephenson
Acclaimed Contributor

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.
A. Clay Stephenson
Acclaimed Contributor

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.
A. Clay Stephenson
Acclaimed Contributor

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.
Dennis Handly
Acclaimed Contributor

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;
}
msbinu
Advisor

Re: Returning array from a function

Thanks all