Returning an array -- help needed

Discussion in 'C++' started by Abhayks, Dec 13, 2008.

  1. Abhayks

    Abhayks Guest

    Hi,
    I am retuning to "C" after a long time and my basics are shatterred.
    Not sure where I am making the mistake.
    Please help.

    #include<stdio.h>
    #include<stdlib.h>

    void foo( char ** ptr)
    {
    int i;
    *ptr = malloc(255); // allocate some memory
    // strcpy( *ptr, "Hello World");
    for( i=0; i<5; i++)
    {
    *ptr='a';
    }

    }

    int main()
    {
    char *ptr = 0;
    // call function with a pointer to pointer
    foo(&ptr );
    printf("%s\n", ptr);
    // free up the memory
    free(ptr);
    return 0;
    }

    Please advise why this is not working?
     
    Abhayks, Dec 13, 2008
    #1
    1. Advertising

  2. Abhayks

    Abhayks Guest

    On Dec 13, 10:13 am, Sam <> wrote:
    > Abhayks writes:
    > > Hi,
    > > I am retuning to "C" after a long time and my basics are shatterred.
    > > Not sure where I am making the mistake.
    > > Please help.

    >
    > > #include<stdio.h>
    > > #include<stdlib.h>

    >
    > > void foo( char ** ptr)
    > > {
    > >   int i;
    > >    *ptr = malloc(255); // allocate some memory
    > >    // strcpy( *ptr, "Hello World");
    > >    for( i=0; i<5; i++)
    > >      {
    > >        *ptr='a';

    >
    > (*ptr)='a';
    >
    > >      }

    >
    > (*ptr)=0;
    >
    >
    >
    > > }

    >
    > > int main()
    > > {
    > >     char *ptr = 0;
    > >     // call function with a pointer to pointer
    > >     foo(&ptr );
    > >     printf("%s\n", ptr);
    > >     // free up the memory
    > >     free(ptr);
    > >     return 0;
    > > }

    >
    > > Please advise why this is not working?

    >
    > 1) Operator precedence. [] carries higher precedence than the unary *
    > operator.
    >
    > 2) The string must be zero-terminated. malloc does not clear allocated
    > memory.
    >
    >  application_pgp-signature_part
    > < 1KViewDownload


    God! how can I miss it.
    I think 2 years of separation did the trick.
    Thanks mate.
    You really are a life saver.
     
    Abhayks, Dec 13, 2008
    #2
    1. Advertising

  3. Abhayks wrote:
    > I am retuning to "C" after a long time and my basics are shatterred.


    This is a C++ group. Different language.
     
    Juha Nieminen, Dec 13, 2008
    #3
  4. On Dec 13, 2:57 am, Abhayks <> wrote:
    > On Dec 13, 10:13 am, Sam <> wrote:
    > > Abhayks writes:
    > > [...]
    > > > Please advise why this is not working?

    >
    > > 1) Operator precedence. [] carries higher precedence than the unary *
    > > operator.

    >
    > > 2) The string must be zero-terminated. malloc does not clear allocated
    > > memory.

    >
    > God! how can I miss it.


    Maybe it would be just as well. There are better ways in C++ than
    using pointers and manual memory handling. If you really did want to
    return an array, as your subject title says, you can do it safely like
    this:

    typedef tr1::array <char, 255> Array;

    Array foo () {
    Array a = {'a', 'a', 'a', 'a', 'a'};
    //strcpy (a.begin (), "Hello World");
    return a;
    }

    int main () {
    Array a = foo ();
    cout << a.begin () << endl;
    return 0;
    }

    But, of course, your example calls for std::string:

    string foo () {
    return "aaaaa"; // "Hello World";
    }

    int main () {
    string s = foo ();
    cout << s << endl;
    return 0;
    }

    Regards,
    Vidar Hasfjord
     
    Vidar Hasfjord, Dec 14, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Nick
    Replies:
    0
    Views:
    1,889
    Thomas Nick
    Jun 13, 2005
  2. Replies:
    11
    Views:
    668
    Christos Georgiou
    May 2, 2006
  3. Ryan Macy

    Ideas needed & help needed!

    Ryan Macy, Jul 19, 2006, in forum: Ruby
    Replies:
    2
    Views:
    518
    Ryan Macy
    Jul 19, 2006
  4. LeTubs
    Replies:
    6
    Views:
    125
    Tad McClellan
    Jan 25, 2004
  5. John Farold
    Replies:
    0
    Views:
    387
    John Farold
    Aug 29, 2012
Loading...

Share This Page