How to set a double pointer as input argument

P

Plissken.s

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(),

}
 
S

santosh

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(),

}
 
B

Bill Pursell

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.
 
N

Nick Keighley

Bill said:
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;
}

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top