473,387 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to set a double pointer as input argument

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
3 4482
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: J | last post by:
Would anyone know if there a type tag to format a double? I have f for floating point, but cannot find one for double.
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
10
by: Robert Palma | last post by:
I'm having trouble figuring out how to pass a pointer to a double array (1 dimensional) to a C function. Declaring array as: double xx; Declaring func. int process( double *input ) Calling...
14
by: ranjmis | last post by:
Hi all, Below is the code wherein I am initializing double dimentional array inside main with string literals. Now I want to display the strings using a function call to which I just want to...
42
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
11
by: aisling.cronin | last post by:
Hi I am using ctime to convert the following string 1144412677847 .... Please could some one to double check if they get the same result as me (The Time is Sun Nov 02 09:11:51 2031). It seems...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
by: Keith Thompson | last post by:
jameskuyper@verizon.net writes: In addition to the advice James gave you, please avoid the term "double pointer" to refer to a pointer to a pointer. A "double pointer" would be a pointer to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.