<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Returning array from a function in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/returning-array-from-a-function/m-p/4980917#M99747</link>
    <description>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.&lt;BR /&gt;&lt;BR /&gt;char *bad_array()&lt;BR /&gt;{&lt;BR /&gt;  char abc[16];&lt;BR /&gt;&lt;BR /&gt;  (void) sprintf("abcdefghijklmno");&lt;BR /&gt;  return(abc);&lt;BR /&gt;} /* bad_array */&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;char *good_array()&lt;BR /&gt;{&lt;BR /&gt;  static char abc[16];&lt;BR /&gt;&lt;BR /&gt;  (void) sprintf("ABCDEFGHIJKLMNO");&lt;BR /&gt;  return(abc);&lt;BR /&gt;} /* bad_array */&lt;BR /&gt;&lt;BR /&gt;int main()&lt;BR /&gt;{&lt;BR /&gt;  (void) printf("Bad (maybe) : %s Good: %s\n",bad_array(),good_array());&lt;BR /&gt;  return(0);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;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. &lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Thu, 25 May 2006 09:41:06 GMT</pubDate>
    <dc:creator>A. Clay Stephenson</dc:creator>
    <dc:date>2006-05-25T09:41:06Z</dc:date>
    <item>
      <title>Returning array from a function</title>
      <link>https://community.hpe.com/t5/operating-system-linux/returning-array-from-a-function/m-p/4980916#M99746</link>
      <description>Hi &lt;BR /&gt;&lt;BR /&gt;I have an array  unsigned char * abc[16]&lt;BR /&gt;&lt;BR /&gt;If I print the contents like below,&lt;BR /&gt;&lt;BR /&gt; for (i = 0; i &amp;lt; 16; i++)&lt;BR /&gt;    printf ("%02x", mdContext-&amp;gt;digest[i]);&lt;BR /&gt;&lt;BR /&gt;I will get the following output&lt;BR /&gt;&lt;BR /&gt;21232f297a57a5a743894a0e4a801fc3&lt;BR /&gt;&lt;BR /&gt;I want to pass the same values to the calling funtion from there i wants to write the same thing to a socket .&lt;BR /&gt;&lt;BR /&gt;I was facing with returing array form tht function .&lt;BR /&gt;&lt;BR /&gt;Hence I did some thing like the following &lt;BR /&gt;&lt;BR /&gt;  RWCString abc;&lt;BR /&gt;  for (int i = 0; i &amp;lt; 16; i++)&lt;BR /&gt;    abc.append( mdContext.digest[i]);&lt;BR /&gt;  cout &amp;lt;&amp;lt;" binu " &amp;lt;&amp;lt; abc &amp;lt;&lt;ENDL&gt;&lt;/ENDL&gt;  return abc.data();&lt;BR /&gt;&lt;BR /&gt;But here I m not getting the correct data ... I m getting some thing like&lt;BR /&gt; ©ý¤¼Þ²)8ül&amp;amp;¿G&lt;BR /&gt;&lt;BR /&gt;can any one help me to retrive proper data &lt;BR /&gt;&lt;BR /&gt;Regards&lt;BR /&gt;BINu &lt;BR /&gt;</description>
      <pubDate>Thu, 25 May 2006 08:56:12 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/returning-array-from-a-function/m-p/4980916#M99746</guid>
      <dc:creator>msbinu</dc:creator>
      <dc:date>2006-05-25T08:56:12Z</dc:date>
    </item>
    <item>
      <title>Re: Returning array from a function</title>
      <link>https://community.hpe.com/t5/operating-system-linux/returning-array-from-a-function/m-p/4980917#M99747</link>
      <description>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.&lt;BR /&gt;&lt;BR /&gt;char *bad_array()&lt;BR /&gt;{&lt;BR /&gt;  char abc[16];&lt;BR /&gt;&lt;BR /&gt;  (void) sprintf("abcdefghijklmno");&lt;BR /&gt;  return(abc);&lt;BR /&gt;} /* bad_array */&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;char *good_array()&lt;BR /&gt;{&lt;BR /&gt;  static char abc[16];&lt;BR /&gt;&lt;BR /&gt;  (void) sprintf("ABCDEFGHIJKLMNO");&lt;BR /&gt;  return(abc);&lt;BR /&gt;} /* bad_array */&lt;BR /&gt;&lt;BR /&gt;int main()&lt;BR /&gt;{&lt;BR /&gt;  (void) printf("Bad (maybe) : %s Good: %s\n",bad_array(),good_array());&lt;BR /&gt;  return(0);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;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. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 25 May 2006 09:41:06 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/returning-array-from-a-function/m-p/4980917#M99747</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2006-05-25T09:41:06Z</dc:date>
    </item>
    <item>
      <title>Re: Returning array from a function</title>
      <link>https://community.hpe.com/t5/operating-system-linux/returning-array-from-a-function/m-p/4980918#M99748</link>
      <description>and as a test of further understanding, try this:&lt;BR /&gt;&lt;BR /&gt;char *Good_array(int i)&lt;BR /&gt;{&lt;BR /&gt;static char abc[16];&lt;BR /&gt;&lt;BR /&gt;(void) sprintf("Good %d",i);&lt;BR /&gt;return(abc);&lt;BR /&gt;} /* Good_array */&lt;BR /&gt;&lt;BR /&gt;int main()&lt;BR /&gt;{&lt;BR /&gt;(void) printf("%s %s %s %s\n",Good_array(1),Good_array(2),Good_array(3),Good_array(4));&lt;BR /&gt;return(0);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;Are the results what you expected?</description>
      <pubDate>Thu, 25 May 2006 09:50:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/returning-array-from-a-function/m-p/4980918#M99748</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2006-05-25T09:50:38Z</dc:date>
    </item>
    <item>
      <title>Re: Returning array from a function</title>
      <link>https://community.hpe.com/t5/operating-system-linux/returning-array-from-a-function/m-p/4980919#M99749</link>
      <description>Ooops, I messed up the sprintf. I left out the 1st parameter.&lt;BR /&gt;&lt;BR /&gt;should be: &lt;BR /&gt;&lt;BR /&gt;char *bad_array()&lt;BR /&gt;{&lt;BR /&gt;char abc[16];&lt;BR /&gt;&lt;BR /&gt;(void) sprintf(abc,"abcdefghijklmno");&lt;BR /&gt;return(abc);&lt;BR /&gt;} /* bad_array */&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;char *good_array()&lt;BR /&gt;{&lt;BR /&gt;static char abc[16];&lt;BR /&gt;&lt;BR /&gt;(void) sprintf(abc,"ABCDEFGHIJKLMNO");&lt;BR /&gt;return(abc);&lt;BR /&gt;} /* good_array */&lt;BR /&gt;&lt;BR /&gt;int main()&lt;BR /&gt;{&lt;BR /&gt;(void) printf("Bad (maybe) : %s Good: %s\n",bad_array(),good_array());&lt;BR /&gt;return(0);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 25 May 2006 11:33:50 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/returning-array-from-a-function/m-p/4980919#M99749</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2006-05-25T11:33:50Z</dc:date>
    </item>
    <item>
      <title>Re: Returning array from a function</title>
      <link>https://community.hpe.com/t5/operating-system-linux/returning-array-from-a-function/m-p/4980920#M99750</link>
      <description>and similarly,&lt;BR /&gt;&lt;BR /&gt;should be: &lt;BR /&gt;&lt;BR /&gt;char *Good_array(int i)&lt;BR /&gt;{&lt;BR /&gt;static char abc[16];&lt;BR /&gt;&lt;BR /&gt;(void) sprintf(abc,"Good %d",i);&lt;BR /&gt;return(abc);&lt;BR /&gt;} /* Good_array */&lt;BR /&gt;&lt;BR /&gt;int main()&lt;BR /&gt;{&lt;BR /&gt;(void) printf("%s %s %s %s\n",Good_array(1),Good_array(2),Good_array(3),Good_array(4));&lt;BR /&gt;return(0);&lt;BR /&gt;}&lt;BR /&gt;</description>
      <pubDate>Thu, 25 May 2006 11:35:08 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/returning-array-from-a-function/m-p/4980920#M99750</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2006-05-25T11:35:08Z</dc:date>
    </item>
    <item>
      <title>Re: Returning array from a function</title>
      <link>https://community.hpe.com/t5/operating-system-linux/returning-array-from-a-function/m-p/4980921#M99751</link>
      <description>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.&lt;BR /&gt;&lt;BR /&gt;If you really really want to return an array by value, you can wrap it in a struct. Or you could use Pascal. ;-)&lt;BR /&gt;&lt;BR /&gt;struct return_array { char arr[16]; }&lt;BR /&gt;struct return_array ret_array(int i) {&lt;BR /&gt;struct return_array abc;&lt;BR /&gt;(void)sprintf(abc.arr,"Good %d",i);&lt;BR /&gt;return abc;&lt;BR /&gt;}</description>
      <pubDate>Thu, 25 May 2006 17:14:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/returning-array-from-a-function/m-p/4980921#M99751</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2006-05-25T17:14:16Z</dc:date>
    </item>
    <item>
      <title>Re: Returning array from a function</title>
      <link>https://community.hpe.com/t5/operating-system-linux/returning-array-from-a-function/m-p/4980922#M99752</link>
      <description>Thanks all</description>
      <pubDate>Wed, 31 May 2006 01:45:40 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/returning-array-from-a-function/m-p/4980922#M99752</guid>
      <dc:creator>msbinu</dc:creator>
      <dc:date>2006-05-31T01:45:40Z</dc:date>
    </item>
  </channel>
</rss>

