pass pointer by reference

Discussion in 'C Programming' started by HSeganfredo@gmail.com, Sep 12, 2007.

  1. Guest

    Folks,

    I want to write a init string function that mallocs an area, fills it
    with a char, sticks a NUL char in the last position and returns it to
    the user.

    So far I noticed that my implementation always works over a NEW memory
    area, and not over the ORIGINAL pointer area, so the initial string
    area is untouched, the pointer is not update to refer to the new
    initiated area...

    What is going on? See my code below...

    int main(){
    char *string;
    initstring(string, '\0', 14); /* create a string filled with NUL
    and a extra NUL at position 15 */
    /* if I check *string here, it is untouched since the 1st statement
    */
    }

    void initstring(char *array, char c, unsigned int size){
    array = malloc((size + 1)* sizeof(char));
    int i;

    for(i=0;i<size;i++){
    /* array = c; array indexing */
    *array = c;
    array++;
    }
    /* array[size+1] = '\0'; array indexing */
    *array = '\0';
    }

    So, why "*array" always get into the function initstring with a
    diferent value (memory address) than when the initistring() function
    is called (according)?

    My environment: Eclipse + CDT + Cygwin (Win32)
    , Sep 12, 2007
    #1
    1. Advertising

  2. HSeganfredo wrote:

    [...]
    > int main(){
    > char *string;
    > initstring(string, '\0', 14); /* create a string filled with NUL
    > and a extra NUL at position 15 */
    > /* if I check *string here, it is untouched since the 1st statement
    > */
    > }
    >

    [...]
    > void initstring(char *array, char c, unsigned int size){
    > array = malloc((size + 1)* sizeof(char));


    You pass a *copy* of the pointer to the initstring function.
    The modification of array in initstring won't affect the string variable
    in main.

    You must pass a pointer to pointer, if you want to modify the pointer.

    initstring(&string, '\0', 14);
    /* ... */
    void initstring(char **parray, char c, unsigned int size){
    char* array;
    array=*parray = malloc((size + 1)* sizeof(char));

    BTW, you should respect the sixth commandment for the C programmer:
    "
    If a function be advertised to return an error code in the event of
    difficulties, thou shalt check for that code, yea, even though the checks
    triple the size of thy code and produce aches in thy typing fingers, for
    if thou thinkest ``it cannot happen to me'', the gods shall surely punish
    thee for thy arrogance.
    "

    You must check the return value of malloc()!

    --
    You can contact me at <>
    André Gillibert, Sep 12, 2007
    #2
    1. Advertising

  3. Martin Wells Guest


    > initstring(string, '\0', 14);



    As someone has already said, you have to pass the address of the
    pointer, i.e. a pointer to a pointer.

    Really though, I'd just return a pointer from the function:

    Unchecked, untested code. . .

    char *const initstring(char const c,size_t const len)
    {
    char *const retval = malloc(len + 1);

    char *p = retval;
    char const *const pnull = retval + len;

    while (p != pnull) *p++ = c;

    *p = 0;

    return retval;
    }


    I use the const in the return value as an indicator to the programmer
    that they might want to hang on to the value.

    Martin
    Martin Wells, Sep 12, 2007
    #3
  4. Guest

    Thanks for the "pointers", worked here perfectly.

    I started to remember that a pointer reference "func(&ptr)" in the
    calling function always implies on a double pointer in the prototype
    (**ptr), one contains the original pointer reference (to char in my
    case) and another one containing the reference to that pointer. It´s a
    somewhat complex subject that makes years I don´t deal with. Thanks.
    , Sep 12, 2007
    #4
  5. pete Guest

    wrote:

    > void initstring(char *array, char c, unsigned int size){
    > array =


    Whenever you see a function definition,
    where a parameter is being overwritten
    without being read first, that's a bad sign.

    --
    pete
    pete, Sep 13, 2007
    #5
  6. Willem Guest

    Martin wrote:
    ) char *const initstring(char const c,size_t const len)
    ) {
    ) char *const retval = malloc(len + 1);
    )
    ) char *p = retval;
    ) char const *const pnull = retval + len;
    )
    ) while (p != pnull) *p++ = c;
    )
    ) *p = 0;
    )
    ) return retval;
    ) }

    I find that a very convoluted way to write:

    if (retval = malloc(len + 1)) {
    memset(retval, c, len);
    retval[c] = 0;
    }
    return retval;


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Sep 13, 2007
    #6
  7. Denis Kasak Guest

    Willem wrote:
    > Martin wrote:
    > ) char *const initstring(char const c,size_t const len)
    > ) {
    > ) char *const retval = malloc(len + 1);
    > )
    > ) char *p = retval;
    > ) char const *const pnull = retval + len;
    > )
    > ) while (p != pnull) *p++ = c;
    > )
    > ) *p = 0;
    > )
    > ) return retval;
    > ) }
    >
    > I find that a very convoluted way to write:
    >
    > if (retval = malloc(len + 1)) {
    > memset(retval, c, len);
    > retval[c] = 0;


    You probably meant retval[len] = 0;

    > }
    > return retval;


    --
    Denis Kasak
    Denis Kasak, Sep 13, 2007
    #7
  8. Willem Guest

    Denis wrote:
    ) Willem wrote:
    )> if (retval = malloc(len + 1)) {
    )> memset(retval, c, len);
    )> retval[c] = 0;
    )
    ) You probably meant retval[len] = 0;
    )
    )> }
    )> return retval;

    It's a well-known Law of the Useniverse that any post which is made to
    correct/improve upon any piece of language must contain a silly error.

    I guess C counts as a language as well for that Law. :)


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Sep 13, 2007
    #8
  9. Guest

    Thanks everybody!
    , Sep 13, 2007
    #9
  10. Martin Wells Guest

    Willem:

    > I find that a very convoluted way to write:
    >
    > if (retval = malloc(len + 1)) {
    > memset(retval, c, len);
    > retval[c] = 0;
    > }
    > return retval;



    You're right I shuda used memset... it slipped my mind that there's a
    Standard Library function for doing that.

    Martin
    Martin Wells, Sep 13, 2007
    #10
  11. Guest

    On Sep 12, 6:24 pm, "André Gillibert"
    <> wrote:
    >
    > BTW, you should respect the sixth commandment for the C programmer:


    Salut André,

    Ou est ce que les commandments de C?

    Lisp 9000
    , Sep 13, 2007
    #11
  12. André Gillibert, Sep 21, 2007
    #12
    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. Jerry
    Replies:
    20
    Views:
    7,881
    Roedy Green
    Sep 9, 2005
  2. Mr A
    Replies:
    111
    Views:
    2,051
  3. Robert
    Replies:
    10
    Views:
    1,352
    E. Robert Tisdale
    Aug 24, 2005
  4. LuB
    Replies:
    6
    Views:
    439
    Mike Wahler
    Sep 23, 2005
  5. A
    Replies:
    7
    Views:
    620
Loading...

Share This Page