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