Hi.

You know, this question is off-topic on the MySQL list.

On Wed, Feb 14, 2001 at 09:54:44AM +0000, [EMAIL PROTECTED] wrote:
> Hi folks,
> 
> I am trying to compile a simple program to play with mysql. But i am
> running into single problems, but i guess easy to solve by too many of
> the wizards here.
> 
> I am too paranoic when compiling my programs, so i activate all warnning
> (at least i try) flags to gnu c compiler.
> 
> The problem occurs when type conversion: type conversion is automatic
> when a functions takes a single pointer to const anything, but when
> there is a double pointer to a const type and i pass double pointer to a
> non const type i run into problem? Why in the later case conversion is
> not automatic? the former is. 

This is a FAQ, see e.g. http://faqs.jmas.co.jp/FAQs/C-faq/faq, section
11.10. For a somewhat better explanation have a look at the C++ FAQ
(and also applies to your C program):
http://reality.sgi.com/austern_mti/std-c++/faq.html#C1

The summary is that one could bypass the const qualifier c() adds (not
in your example, but in general).

> Here is the simple code (just to test):
> 
> #include <stdio.h>
> 
> int
> t(const int *i)
> {
>         printf("%d\n", *i);
>         return 0;
> }
> 
> void
> c(const char **reg)

With C++, you can declare it as

void c(const char * const* reg)

but this does not seem to be allowed in C (I am not used to C anymore,
but gcc 2.95.3 complains).

> {
>         while (*reg)
>                 printf("%s\n", *reg++);
> }
> 
> int
> main(int argc, char *argv[])
> {
>         int     i;
>         char    *reg[] = {
>                         "Gustavo",
>                         "Vieira",
>                         "Goncalves",
>                         "Coelho", "Rios", NULL};

The other way is to declare "reg" to what is really is, instead:

const char *reg[] = ...

This will also work with C.

The solution of the C-FAQ is dirty (as it opens the hole again because
of which the auto cast is not made) and should IMHO be avoided, if
possible.

Bye,

        Benjamin.


>         i = 10;
> 
>         (void) t(&i);
>         c(reg);
> 
>         return 0;
> }
> 
> 
> But when compiling:
> 
> grios@etosha$ cc -ansi -pedantic -Wconversion -Wall -Werror t.c
> cc1: warnings being treated as errors
> t.c: In function `main':
> t.c:30: warning: passing arg 1 of `c' from incompatible pointer type
> grios@etosha$ 
[...]

---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to