Re: help !!!

From: "Nigel J(dot) Andrews" <nandrews(at)investsystems(dot)co(dot)uk>
To: "Goulet, Dick" <DGoulet(at)vicr(dot)com>
Cc: "apellido jr(dot), wilfredo p(dot)" <apellido(at)mactan(dot)ph>, pgsql-interfaces(at)postgresql(dot)org
Subject: Re: help !!!
Date: 2004-07-01 06:31:27
Message-ID: Pine.LNX.4.21.0407010712210.12226-100000@ponder.fairway2k.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces


On Wed, 30 Jun 2004, Goulet, Dick wrote:
> PGgetValue returns a character pointer, so if you have:
>
> char *reply_string = NULL;
>
> reply_string = PQgetResult(rex,0,0);

Never used the C interface so I'll assume getValue is the function name...

>
> hello how can i assign to certain variable the output of PQgetvalue(res, 0,
> 1)? example i want to do like this ...
>
> a = PQgetvalue(res, 0, 1)
> printf("Grade : %s \n\n\n", a );

That was answered above.

>
> and then after assigning the value of PQgetvalue(res, 0, 1) to a then ...
>
> b = a \ 2;
>
> but it is possible to divide the output of "a" where it is a var and "b" is
> integer?

Sure. The complexity depends how safety conscious you are.

double d;
char *s;

s = PQgetValue(...);

d = strtod(s,NULL) / 2.0;

will give zero or a perhaps unexpected result if s points to something that
isn't convertable to a double in it's entirety. Read up on strtod() but
basically, "abcde56.98" will give 0 and "56.98abcde" will give 56.98. You
supply a second argument to strtod, as below, to determine how successful the
conversion was.

char *end;

d = strtod(s,&end);
if (end != s+strlen(s))
{
/* string incompletely converted */
}

or the above test could be replaced with if (*end) {... which is how I'd write
it.

Then there's also checks for under and overflow:

errno=0;
d=strtod(s,&end);
if (d == 0 && errno == ERANGE) { /* underflow */ }
if ((d == HUGE_VAL || d == -HUGE_VAL) && errno == ERANGE) { /* overflow */ }

I'm pretty sure that errno should not be set if there is no under or overflow
so those tests can be adjusted to take advantage of that fact.

Hope that helps. Remember, man pages are your friends and man -k can be a
useful starting point if you can think of a a keyword. Consider getting a book
on C, sorry can't recomend one, and/or searching the web for tutorials.

--
Nigel Andrews

In response to

Browse pgsql-interfaces by date

  From Date Subject
Next Message apellido jr., wilfredo p. 2004-07-01 13:43:47 Re: help !!!
Previous Message Nigel J. Andrews 2004-06-30 22:29:32 Re: Released updated Tcl interfaces: pgin.tcl-2.1.0,