Passing parameters to a C function - Mailing list pgsql-hackers
From | Rodrigo Sakai |
---|---|
Subject | Passing parameters to a C function |
Date | |
Msg-id | 000001c7a2c6$75570510$9200a8c0@NOTEBOOKSAKAI Whole thread Raw |
Responses |
Re: Passing parameters to a C function
|
List | pgsql-hackers |
<div class="Section1"><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial"> Hello,</span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial"> I have a question about passing parameters to a C function. Imagine the example by PostgreSQL:</span></font><pclass="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial"> </span></font><p class="MsoNormal" style="margin-left:.5in"><font face="Courier New" size="2"><span style="font-size:10.0pt;font-family:"CourierNew"">PG_FUNCTION_INFO_V1(complex_add);</span></font><p class="MsoNormal" style="margin-left:.5in"><fontface="Courier New" size="2"><span style="font-size:10.0pt;font-family:"Courier New""> </span></font><pclass="MsoNormal" style="margin-left:.5in"><font face="Courier New" size="2"><span style="font-size:10.0pt;font-family:"CourierNew"">Datum</span></font><p class="MsoNormal" style="margin-left:.5in"><fontface="Courier New" size="2"><span style="font-size:10.0pt;font-family:"Courier New"">complex_add(PG_FUNCTION_ARGS)</span></font><pclass="MsoNormal" style="margin-left:.5in"><font face="Courier New" size="2"><spanstyle="font-size:10.0pt;font-family:"Courier New"">{</span></font><p class="MsoNormal" style="margin-left:.5in"><fontface="Courier New" size="2"><span style="font-size:10.0pt;font-family:"Courier New""> Complex *a = (Complex *) PG_GETARG_POINTER(0);</span></font><p class="MsoNormal" style="margin-left:.5in"><fontface="Courier New" size="2"><span style="font-size:10.0pt;font-family:"Courier New""> Complex *b = (Complex *) PG_GETARG_POINTER(1);</span></font><p class="MsoNormal" style="margin-left:.5in"><fontface="Courier New" size="2"><span style="font-size:10.0pt;font-family:"Courier New""> Complex *result;</span></font><p class="MsoNormal" style="margin-left:.5in"><font face="Courier New" size="2"><spanstyle="font-size:10.0pt;font-family:"Courier New""> </span></font><p class="MsoNormal" style="margin-left:.5in"><fontface="Courier New" size="2"><span style="font-size:10.0pt;font-family:"Courier New""> result = (Complex *) palloc(sizeof(Complex));</span></font><p class="MsoNormal" style="margin-left:.5in"><fontface="Courier New" size="2"><span style="font-size:10.0pt;font-family:"Courier New""> result->x = a->x + b->x;</span></font><p class="MsoNormal" style="margin-left:.5in"><font face="CourierNew" size="2"><span style="font-size:10.0pt;font-family:"Courier New""> result->y = a->y + b->y;</span></font><pclass="MsoNormal" style="margin-left:.5in"><font face="Courier New" size="2"><span style="font-size:10.0pt;font-family:"CourierNew""> PG_RETURN_POINTER(result);</span></font><p class="MsoNormal" style="margin-left:.5in"><fontface="Courier New" size="2"><span style="font-size:10.0pt;font-family:"Courier New"">}</span></font><pclass="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial"> So, in the same .C file I have to write another function that calls this one, for example: </span></font><pclass="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial"> </span></font><p class="MsoNormal" style="margin-left:.5in"><font face="Courier New" size="2"><span style="font-size:10.0pt;font-family:"CourierNew"">void</span></font><p class="MsoNormal" style="margin-left:.5in"><font face="CourierNew" size="2"><span style="font-size:10.0pt;font-family:"Courier New"">test_main()</span></font><p class="MsoNormal"style="margin-left:.5in"><font face="Courier New" size="2"><span style="font-size:10.0pt;font-family:"CourierNew"">{</span></font><p class="MsoNormal" style="margin-left:.5in"><font face="CourierNew" size="2"><span style="font-size:10.0pt;font-family:"Courier New""> Complex *a;</span></font><p class="MsoNormal"style="margin-left:.5in"><font face="Courier New" size="2"><span style="font-size:10.0pt;font-family:"CourierNew""> Complex *b;</span></font><p class="MsoNormal" style="margin-left:.5in"><fontface="Courier New" size="2"><span style="font-size:10.0pt;font-family:"Courier New""> </span></font><pclass="MsoNormal" style="margin-left:.5in"><font face="Courier New" size="2"><span style="font-size:10.0pt;font-family:"CourierNew""> a = complex_in("(4.01, 3.77 )");</span></font><p class="MsoNormal"style="margin-left:.5in"><font face="Courier New" size="2"><span style="font-size:10.0pt;font-family:"CourierNew""> printf("a = %s\n", complex_out(a));</span></font><p class="MsoNormal"style="margin-left:.5in"><font face="Courier New" size="2"><span style="font-size:10.0pt;font-family:"CourierNew""> </span></font><p class="MsoNormal" style="margin-left:.5in"><font face="CourierNew" size="2"><span style="font-size:10.0pt;font-family:"Courier New""> b = complex_in("(1.0,2.0)");</span></font><pclass="MsoNormal" style="margin-left:.5in"><font face="Courier New" size="2"><spanstyle="font-size:10.0pt;font-family:"Courier New""> printf("b = %s\n", complex_out(b));</span></font><pclass="MsoNormal" style="margin-left:.5in"><font face="Courier New" size="2"><span style="font-size:10.0pt;font-family:"CourierNew""> </span></font><p class="MsoNormal" style="margin-left:.5in"><font face="CourierNew" size="2"><span style="font-size:10.0pt;font-family:"Courier New""> printf("a + b = %s\n", complex_out(complex_add(a,b)));</span></font><pclass="MsoNormal" style="margin-left:.5in"><font face="Courier New" size="2"><spanstyle="font-size:10.0pt;font-family:"Courier New"">}</span></font><p class="MsoNormal" style="margin-left:.5in"><fontface="Courier New" size="2"><span style="font-size:10.0pt;font-family:"Courier New""> </span></font><pclass="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial">But using version 1 calling convention it won’t work! So, how can I pass the ‘a’ and ‘b’ variables incomplex_add(?, ?)?</span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial">Thanks in advance!</span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; font-family:Arial"> </span></font></div>
pgsql-hackers by date: