Pointers to char pointers in functions

Discussion in 'C Programming' started by newbie, Sep 18, 2006.

  1. newbie

    newbie Guest

    Can someone please give me some tips?

    I have a function f(). In function f() I allocate memory for a char
    array. Now the memory I allocate in f() must be assigned to a char*
    outside of f() so how would I pass in a char* and assign it the address
    of the allocated memory, example:

    void f(char* p)
    {
    char* a=(char* )malloc(10);
    // do some error checking;
    return(0);
    }

    int main(void)
    {
    char *b;

    // call f()...

    return(0);
    }

    How would I call f() so that afterwards b, in main(), points to the
    memory allocated in function f()? Do I need to change the
    declaration/definition of f() to something like:

    void f(char** p)...

    Or other and how would I call f()? Please note this is not any
    howework or such I am purely learning C in my spare time.

    Anything appreciated.

    Steve
     
    newbie, Sep 18, 2006
    #1
    1. Advertising

  2. newbie said:

    > Can someone please give me some tips?
    >
    > I have a function f(). In function f() I allocate memory for a char
    > array. Now the memory I allocate in f() must be assigned to a char*
    > outside of f() so how would I pass in a char* and assign it the address
    > of the allocated memory, example:
    >
    > void f(char* p)
    > {
    > char* a=(char* )malloc(10);
    > // do some error checking;
    > return(0);
    > }


    Not like that!

    Firstly, drop your cast on malloc. It's not required, and can conceal a bug.
    If your compiler seems to be telling you that it /is/ required, check that
    you've got <stdlib.h> included. Chances are you haven't. Include it, and
    the warning message will go away. If it doesn't, then you're accidentally
    using a C++ compiler instead of a C compiler.

    Secondly, let's answer your question. Here's the simple way:

    #include <stdlib.h>

    char *f(void)
    {
    return malloc(10);
    }

    int main(void)
    {
    char *b = f();
    if(b != NULL)
    {
    /* use b for whatever you wanted 10 bytes of memory for, and then... */
    free(b);
    }
    return 0;
    }

    And here's the less simple way:

    #include <stdlib.h>

    void f(char **a) /* note the >>>two<<< stars */
    {
    *a = malloc(10);
    }

    int main(void)
    {
    char *b = NULL;
    f(&b);

    if(b != NULL)
    {
    /* use b for whatever you wanted 10 bytes of memory for, and then... */
    free(b);
    }
    return 0;
    }


    <snip>

    > How would I call f() so that afterwards b, in main(), points to the
    > memory allocated in function f()? Do I need to change the
    > declaration/definition of f() to something like:
    >
    > void f(char** p)...


    Yes. See above.

    > Or other and how would I call f()?


    Again, see above.

    > Please note this is not any
    > howework or such I am purely learning C in my spare time.


    In this case, it wouldn't have mattered if it were your homework, since it
    was clear from your question that you were trying to understand, and had
    made a fair effort on your own account. This newsgroup is not against
    helping with homework. It's against laziness and dishonesty. You failed to
    meet the qualifying criteria for being an Enemy of comp.lang.c. ;-)

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Sep 18, 2006
    #2
    1. Advertising

  3. newbie

    newbie Guest

    Many thanks for the response Richard. I will from now on drop the
    cast's for malloc unless the compiler complains (I generally use MS's
    cl). I do include stdlib.h when using malloc I have just missed it
    from my example (sorry about that).

    Thanks for such a prompt response!

    Richard Heathfield wrote:
    > newbie said:
    >
    > > Can someone please give me some tips?
    > >
    > > I have a function f(). In function f() I allocate memory for a char
    > > array. Now the memory I allocate in f() must be assigned to a char*
    > > outside of f() so how would I pass in a char* and assign it the address
    > > of the allocated memory, example:
    > >
    > > void f(char* p)
    > > {
    > > char* a=(char* )malloc(10);
    > > // do some error checking;
    > > return(0);
    > > }

    >
    > Not like that!
    >
    > Firstly, drop your cast on malloc. It's not required, and can conceal a bug.
    > If your compiler seems to be telling you that it /is/ required, check that
    > you've got <stdlib.h> included. Chances are you haven't. Include it, and
    > the warning message will go away. If it doesn't, then you're accidentally
    > using a C++ compiler instead of a C compiler.
    >
    > Secondly, let's answer your question. Here's the simple way:
    >
    > #include <stdlib.h>
    >
    > char *f(void)
    > {
    > return malloc(10);
    > }
    >
    > int main(void)
    > {
    > char *b = f();
    > if(b != NULL)
    > {
    > /* use b for whatever you wanted 10 bytes of memory for, and then... */
    > free(b);
    > }
    > return 0;
    > }
    >
    > And here's the less simple way:
    >
    > #include <stdlib.h>
    >
    > void f(char **a) /* note the >>>two<<< stars */
    > {
    > *a = malloc(10);
    > }
    >
    > int main(void)
    > {
    > char *b = NULL;
    > f(&b);
    >
    > if(b != NULL)
    > {
    > /* use b for whatever you wanted 10 bytes of memory for, and then... */
    > free(b);
    > }
    > return 0;
    > }
    >
    >
    > <snip>
    >
    > > How would I call f() so that afterwards b, in main(), points to the
    > > memory allocated in function f()? Do I need to change the
    > > declaration/definition of f() to something like:
    > >
    > > void f(char** p)...

    >
    > Yes. See above.
    >
    > > Or other and how would I call f()?

    >
    > Again, see above.
    >
    > > Please note this is not any
    > > howework or such I am purely learning C in my spare time.

    >
    > In this case, it wouldn't have mattered if it were your homework, since it
    > was clear from your question that you were trying to understand, and had
    > made a fair effort on your own account. This newsgroup is not against
    > helping with homework. It's against laziness and dishonesty. You failed to
    > meet the qualifying criteria for being an Enemy of comp.lang.c. ;-)
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999
    > http://www.cpax.org.uk
    > email: rjh at above domain (but drop the www, obviously)
     
    newbie, Sep 18, 2006
    #3
  4. newbie

    Default User Guest

    newbie wrote:

    > Many thanks for the response Richard.


    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or:
    <http://www.caliburn.nl/topposting.html>




    Brian
     
    Default User, Sep 18, 2006
    #4
  5. newbie

    newbie Guest

    Default User wrote:
    > newbie wrote:
    >
    > > Many thanks for the response Richard.

    >
    > Please don't top-post. Your replies belong following or interspersed
    > with properly trimmed quotes. See the majority of other posts in the
    > newsgroup, or:
    > <http://www.caliburn.nl/topposting.html>
    >
    >
    >
    >
    > Brian


    Sorry, understood :)
     
    newbie, Sep 18, 2006
    #5
  6. newbie

    Default User Guest

    newbie wrote:

    >
    > Default User wrote:


    > > Please don't top-post.


    > Sorry, understood :)



    Sure, thanks.




    Brian
     
    Default User, Sep 18, 2006
    #6
  7. "newbie" <> writes:
    > Many thanks for the response Richard. I will from now on drop the
    > cast's for malloc unless the compiler complains (I generally use MS's
    > cl). I do include stdlib.h when using malloc I have just missed it
    > from my example (sorry about that).


    And if the compiler *does* complain, stop and figure out why it's
    complaining before you add a cast to shut it up.

    Never add a cast for the sole purpose of inhibiting a compiler
    warning. A cast effectively tells the compiler, "trust me, I know
    what I'm doing". Inserting a cast when you *don't* really know what
    you're doing is an excellent way to shoot yourself in the foot.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 19, 2006
    #7
  8. In article <>,
    Keith Thompson <> wrote:

    >Never add a cast for the sole purpose of inhibiting a compiler
    >warning. A cast effectively tells the compiler, "trust me, I know
    >what I'm doing". Inserting a cast when you *don't* really know what
    >you're doing is an excellent way to shoot yourself in the foot.


    A more memorable way of putting this is:

    The cast is how you tell the compiler "I know that's my foot, but I'm
    very carefully aiming between the toes."


    dave

    --
    Dave Vandervies
    uch compilers usually allow <code samples> respectively as "please don't
    nag, I _know_ that's my foot, but I'm very carefully aiming between the toes."
    --Alan Bellingham in the scary devil monastery
     
    Dave Vandervies, Sep 23, 2006
    #8
  9. (Dave Vandervies) writes:
    > In article <>,
    > Keith Thompson <> wrote:
    >
    >>Never add a cast for the sole purpose of inhibiting a compiler
    >>warning. A cast effectively tells the compiler, "trust me, I know
    >>what I'm doing". Inserting a cast when you *don't* really know what
    >>you're doing is an excellent way to shoot yourself in the foot.

    >
    > A more memorable way of putting this is:
    >
    > The cast is how you tell the compiler "I know that's my foot, but I'm
    > very carefully aiming between the toes."


    I like it!

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 23, 2006
    #9
  10. newbie wrote:
    > Can someone please give me some tips?
    >
    > I have a function f(). In function f() I allocate memory for a char
    > array. Now the memory I allocate in f() must be assigned to a char*
    > outside of f() so how would I pass in a char* and assign it the address
    > of the allocated memory,


    If the sole purpose of your function is to do checked memory allocation
    I would define a generic function, something like:

    #define NEW(p, n) \
    p = malloc(n); \
    if (p == NULL) { \
    /* do something */ \
    }

    If not, I think the most flexible solution is to to let the caller
    provide a large enough character array instead of allocating memory
    inside the function:

    void f(char *result)
    {
    /* Assign values to the array pointed at by result. */
    }


    August
     
    August Karlstrom, Sep 24, 2006
    #10
    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. wwj
    Replies:
    7
    Views:
    594
  2. wwj
    Replies:
    24
    Views:
    2,570
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    504
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    624
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,149
    Ian Collins
    May 9, 2006
Loading...

Share This Page