Pointer trouble

Discussion in 'C Programming' started by Kelvin Moss, Dec 17, 2004.

  1. Kelvin Moss

    Kelvin Moss Guest

    Hi all,

    I am facing a problem with a library that allocates memory internally.
    My basic problem is how to reflect changes in pointer values back in
    main.

    My library routine allocates dynamically and returns char ** (array of
    strings). To track changes in main I pass a char*** and dereference the
    passed value in foo (this is unsafe, don't know what it's pointing to).

    void foo (char ***i, char ***j)
    {
    /* i and j are allocated dynamically here */
    **i = lib_routine();
    ....
    **j = lib_routine();

    }
    /* In main, i want to know changes made to pointer values */
    int main(void)
    {
    char ***a, char ***b;
    foo(a, b)
    /* Do something with *a[0] and *b[0] */
    }

    But isn't it unsafe (I dereference an unknown pointer when I write **i
    = lib_routine();) ? How do I track changes in pointer values in a
    called function ?

    Thanks

    PS - I wanting something on the lines of a reference to pointer in C++.
     
    Kelvin Moss, Dec 17, 2004
    #1
    1. Advertising

  2. Kelvin Moss

    Kelvin Moss Guest

    void foo (char ***i, char ***j)
    >{
    >/* i and j are allocated dynamically here */
    >**i = lib_routine();

    Should be *i = lib_routine();
    >...
    >**j = lib_routine();

    Should be *j = lib_routine();



    }
     
    Kelvin Moss, Dec 17, 2004
    #2
    1. Advertising

  3. On Fri, 17 Dec 2004 01:42:14 -0800, Kelvin Moss wrote:

    > Hi all,
    >
    > I am facing a problem with a library that allocates memory internally.
    > My basic problem is how to reflect changes in pointer values back in
    > main.
    >
    > My library routine allocates dynamically and returns char ** (array of
    > strings). To track changes in main I pass a char*** and dereference the
    > passed value in foo (this is unsafe, don't know what it's pointing to).


    It is up to the caller to pass a pointer to something valid.

    > void foo (char ***i, char ***j)
    > {
    > /* i and j are allocated dynamically here */
    > **i = lib_routine();


    > ...
    > **j = lib_routine();


    That should be *i and *j

    > }
    > /* In main, i want to know changes made to pointer values */
    > int main(void)
    > {
    > char ***a, char ***b;
    > foo(a, b)


    But your caller here isn't passing a valid pointer, it is passing
    uninitialised values which is always an error. What you need is

    char **a, char **b;
    foo(&a, &b);

    which is passing valid addresses if char ** objects that foo can write to.

    >/* Do something with *a[0] and *b[0] */


    You now can.

    Lawrence
     
    Lawrence Kirby, Dec 17, 2004
    #3
  4. Kelvin Moss

    Kelvin Moss Guest

    >char **a, char **b;
    > foo(&a, &b);



    >which is passing valid addresses if char ** objects that foo can write

    to.

    oh yes..thanks much. I guess too many pointers and lack of sleep
    confused me :)
     
    Kelvin Moss, Dec 17, 2004
    #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. Replies:
    10
    Views:
    740
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    874
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,324
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    656
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    494
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page