How to set a double pointer as input argument

Discussion in 'C Programming' started by Plissken.s@gmail.com, Mar 17, 2007.

  1. Guest

    Hi,

    I have a structure like this:

    typedef struct {
    char* buffer;
    int len;
    } buffer_t;

    static buffer_t* buffer[BUFSIZE];

    And I want to implement a function which set the input pointer, points
    to the first element of buffer.

    int getItem(buffer_t** itemp) {
    //
    *itemp = &(buffer[0]);
    return 1;
    }

    I can't get the following code to work,

    main() {

    buffer_t* p;
    getItem(p);
    // p should points to buffer[0] after return from getItem(),

    }
     
    , Mar 17, 2007
    #1
    1. Advertising

  2. santosh Guest

    wrote:
    > Hi,
    >
    > I have a structure like this:
    >
    > typedef struct {
    > char* buffer;
    > int len;


    Wouldn't size_t be a better type for storing the buffer length?

    > } buffer_t;
    >
    > static buffer_t* buffer[BUFSIZE];


    This declares an array BUFSIZE of pointers to type buffer_t.

    > And I want to implement a function which set the input pointer, points
    > to the first element of buffer.
    >
    > int getItem(buffer_t** itemp) {
    > //
    > *itemp = &(buffer[0]);


    Try:

    *itemp = buffer[0];

    > return 1;
    > }
    >
    > I can't get the following code to work,
    >
    > main() {


    int main(void) {

    > buffer_t* p;
    > getItem(p);


    getItem(&p);

    > // p should points to buffer[0] after return from getItem(),
    >
    > }
     
    santosh, Mar 17, 2007
    #2
    1. Advertising

  3. Bill Pursell Guest

    On 17 Mar, 18:01, "" <> wrote:
    >
    > typedef struct {
    > char* buffer;
    > int len;
    > } buffer_t;
    >
    > static buffer_t* buffer[BUFSIZE];
    >
    > And I want to implement a function which set the input pointer, points
    > to the first element of buffer.
    >
    > int getItem(buffer_t** itemp) {
    > *itemp = &(buffer[0]);
    > return 1;
    > }
    >
    > I can't get the following code to work,
    >
    > main() {
    >
    > buffer_t* p;
    > getItem(p);
    > // p should points to buffer[0] after return from getItem(),
    > }


    You'll need to call getItem( &p );
    and getItem should read:
    *itemp = buffer[0]; (&buffer[0] is
    a buffer_t **, so for the assignment
    *itemp = &buffer[0] to be meaningful, itemp
    would have to be of type buffer_t ***.)

    Two stylistic points:
    1. rather than &buffer[0],why not just say "*itemp = buffer;"?
    2. Why the typedef? just define the struct, and use it:

    struct buff {
    char *buffer;
    size_t length;
    };

    int getItem( struct buff **itemp )
    {
    *itemp = buffer[0];
    ....

    IMO, using the typedef is unnecessary obfuscation.

    --
    Bill Pursell
     
    Bill Pursell, Mar 18, 2007
    #3
  4. Bill Pursell wrote:
    > On 17 Mar, 18:01, "" <> wrote:


    > > typedef struct {
    > > char* buffer;
    > > int len;
    > > } buffer_t;
    > >
    > > static buffer_t* buffer[BUFSIZE];
    > >
    > > And I want to implement a function which set the input pointer, points
    > > to the first element of buffer.
    > >
    > > int getItem(buffer_t** itemp) {
    > > *itemp = &(buffer[0]);
    > > return 1;
    > > }


    <snip>

    > Two stylistic points:
    > 1. rather than &buffer[0],why not just say "*itemp = buffer;"?
    > 2. Why the typedef? just define the struct, and use it:
    >
    > struct buff {
    > char *buffer;
    > size_t length;
    > };
    >
    > int getItem( struct buff **itemp )
    > {
    > *itemp = buffer[0];
    > ...
    >
    > IMO, using the typedef is unnecessary obfuscation.


    ah, but the trouble with stylistic points is there's always someone
    to
    disagree. Today it is me. Why type "struct buff" when a simple
    typedef
    will hide the ugly syntax? And you have to type less. IMO C++ got
    this
    right in that you can omit the struct keyword when decalring structs.
    IMO, using struct is unnecessary obfuscation.

    I think C programmers are roughly evenly divided on this one.
    So follow the project standard if it has one, otherwise pick the one
    you like and follow it consistently.


    --
    Nick Keighley
     
    Nick Keighley, Mar 19, 2007
    #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. Sydex
    Replies:
    12
    Views:
    6,568
    Victor Bazarov
    Feb 17, 2005
  2. Replies:
    10
    Views:
    717
    Chris Torek
    Feb 4, 2005
  3. jimjim
    Replies:
    16
    Views:
    853
    Jordan Abel
    Mar 28, 2006
  4. Vijai Kalyan
    Replies:
    4
    Views:
    724
    Vijai Kalyan
    Nov 8, 2005
  5. asker123
    Replies:
    0
    Views:
    1,321
    asker123
    Jun 27, 2009
Loading...

Share This Page