- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Returning array from a function
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2006 01:56 AM
тАО05-25-2006 01:56 AM
Returning array from a function
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 <
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
- Tags:
- ARRAY
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2006 02:41 AM
тАО05-25-2006 02:41 AM
Re: Returning array from a function
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2006 02:50 AM
тАО05-25-2006 02:50 AM
Re: Returning array from a function
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2006 04:33 AM
тАО05-25-2006 04:33 AM
Re: Returning array from a function
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2006 04:35 AM
тАО05-25-2006 04:35 AM
Re: Returning array from a function
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2006 10:14 AM
тАО05-25-2006 10:14 AM
Re: Returning array from a function
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-30-2006 06:45 PM
тАО05-30-2006 06:45 PM