Luca Marletta
Luca Marletta

Reputation: 457

PostgreSQL C function get argument varchar type

how can I get an arg varchar from PG_FUNCTION_ARGS?

I whant to contruct this query but nor with PG_GETARG_VARCHAR_P neither with PG_GETARG_TEXT_P works.

  sprintf(query,"SELECT abp1::real, abp2::real "
                "FROM imu.calcolo WHERE cf = %s;", PG_GETARG_VARCHAR_P(0));

I get the WARNING compiling

warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘struct VarChar *’ [-Wformat]

and after a strange content for the PG_GETARG_VARCHAR_P(0), it is 'P' ??

can someone give an hint?

Upvotes: 1

Views: 405

Answers (1)

Claudi
Claudi

Reputation: 5416

Use VARDATA:

VARDATA(PG_GETARG_VARCHAR_P(0))

EDIT

Remember to also use VARSIZE to get the length of data since as far as I know, data is not null-terminated and sprintf would blow up the destination buffer.

Further information here: http://www.postgresql.org/docs/8.3/static/xfunc-c.html

Upvotes: 1

Related Questions