Sample Header Ad - 728x90

PostgreSQL How do I convert struct text to plain *char in C functions?

3 votes
2 answers
3389 views
I'm using my internal C function which doesn't know about postgresql's text struct, how do I pass text argument when char * expected? #include #include #include "postgres.h" #include "fmgr.h" #ifdef PG_MODULE_MAGIC PG_MODULE_MAGIC; #endif PG_FUNCTION_INFO_V1(fun); Datum fun(PG_FUNCTION_ARGS) { text *str1, *str2; str1 = PG_GETARG_TEXT_P(0); str2 = PG_GETARG_TEXT_P(1); FILE *fp = fopen("/tmp/fun.log", "w"); fprintf(fp, "%s\n", VARDATA_ANY(str1)); fprintf(fp, "%s\n", VARDATA_ANY(str2)); fclose(fp); PG_RETURN_INT32(0); } Here's what xxd sees in log file: 0000000: 666f 6f74 6261 6c6c c88d 7409 70d3 6b09 football..t.p.k. 0000010: 080a 7a6f 6f6d 0a ..zoom. After executing: SELECT * FROM fun('football','zoom'); Why junk after football?
Asked by Moe (41 rep)
Jan 18, 2013, 02:12 PM
Last activity: Feb 3, 2020, 01:28 PM