More problems with arrays !!!!! - Mailing list pgsql-general

From Carlos Peralta Ramirez
Subject More problems with arrays !!!!!
Date
Msg-id 373812B9.794EB6CC@hera.inf.ucv.cl
Whole thread Raw
List pgsql-general
Hi,
I tried to create a function in C  that receive an array X like
argument, and return ane element of  him.

Here is the C code ->

#include <pgsql/postgres.h>
#include <stdio.h>

int4 array_1(int4 x[]){
                                        return(x[1]);
}

int4 array_2(int4 *x){
                                        return(x[1]);
}

int4 array_3(int4 x[]){
                                        return(*(x+1));
}

int4 array_4(int4 *x){
                                        return(*(x+1));
}

/* Note, this functions are tests only,*/

then I ceate this functions in Postgresql ->

testdb=> create function array_1(_int4) returns int4 as
'/var/lib/pgsql/funciones/arraystest.so' language 'c';
CREATE
testdb=> create function array_2(_int4) returns int4 as
'/var/lib/pgsql/funciones/arraystest.so' language 'c';
CREATE
testdb=> create function array_3(_int4) returns int4 as
'/var/lib/pgsql/funciones/arraystest.so' language 'c';
CREATE
testdb=> create function array_4(_int4) returns int4 as
'/var/lib/pgsql/funciones/arraystest.so' language 'c';
CREATE

Therefore I can call the functions , but I hope another results .....

testdb=> select array_1('{1,2,3,4}');
array_1
----
  40
(1 row)

testdb=> select array_2('{1,2,3,4}');
array_2
----
  40
(1 row)

testdb=> select array_3('{1,2,3,4}');
array_3
----
  40
(1 row)

testdb=> select array_4('{1,2,3,4}');
array_4
----
  40
(1 row)

This means that the four functions returns the same value

testdb=> select array_1('{1,4}');
array_1
----
  32
(1 row)

testdb=> select array_1('{1,1}');
array_1
----
  32
(1 row)

testdb=> select array_1('{3,2,3,3,4,5,6}');
array_1
----
  52
(1 row)

testdb=> select array_1('{}');
array_1
----
  12
(1 row)

testdb=> select array_1('{1}');
array_1
----
  28
(1 row)

It seems to be , this functions returns the bytes asigned to them.

What can I do, for get the element (e.g. the fisrt) of the array?


Thanks ,

PD: I want to do this, to make a function that return if a element  is
or not into an array.


pgsql-general by date:

Previous
From: Richard Trice
Date:
Subject: HELP: DBI and Transactions?
Next
From: Andy Lewis
Date:
Subject: Weird datetime problem