By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

How to set a double pointer as input argument

P: n/a
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 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Pl********@gmail.com 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(),

}
Mar 17 '07 #2

P: n/a
On 17 Mar, 18:01, "Plisske...@gmail.com" <Plisske...@gmail.comwrote:
>
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

Mar 18 '07 #3

P: n/a
Bill Pursell wrote:
On 17 Mar, 18:01, "Plisske...@gmail.com" <Plisske...@gmail.comwrote:
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

Mar 19 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.