public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/49748] New: char * const * cannot be assigned to char const * const *
@ 2011-07-14 16:09 gcc at misc dot lka.org.lu
2011-07-14 16:44 ` [Bug c/49748] " redi at gcc dot gnu.org
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: gcc at misc dot lka.org.lu @ 2011-07-14 16:09 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49748
Summary: char * const * cannot be assigned to char const *
const *
Product: gcc
Version: 4.4.3
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: gcc@misc.lka.org.lu
Trying to assign a pointer value A with type "char * const *" into a variable B
declared as "char const * const * " triggers a warning, although the
const-yness of the type is _increased_ not decreased.
Such a thing may be needed when trying to call a function that displays a list
of strings:
void displayList(char const * const * list)
{
/* display list */
}
The char const * declaration is needed if somewhere in the program, this
function is called with an array of static strings:
char const *list1[] = { "hello", "world", NULL };
displayList(list1);
However, if elsewhere in the program, we want to call the function with a list
of _dynamically_ allocated strings, we get a "initialization from incompatible
pointer type" warning:
char **list2;
for(i=0; i< N; i++)
list2[i] = strdup( some expression );
...
displayList(list2);
for(i=0; i<N; i++)
free(list2[i]);
Because of the free(), we cannot declare list2[] as char const ** , but then
this restriction on _adding_ the const qualifier during assignment or parameter
passing is causing trouble.
There is a very similar case, where such a restriction on adding a const
qualifier makes sense:
assiging char ** to char const **
(i.e. the case where in the target, only the string _contents_ are const, but
not the string pointers themselves).
In this case, the assignment warning would be appropriate, in order to prevent
the following:
char *launderConst(char const *in)
{
char * source[1];
char const ** target = source;
target[0] = in;
return source[0];
}
However if the target list itself is const (rather than just its string
contents), we would never be able to do this:
char const *const* target = source;
target[0] = in;
So why the warning about the first line, if the second one is forbidden anyways
(assigning a value to a const)?
Or is there another way of declaring the displayList() function without
triggering a warning in any of both use-cases?
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/49748] char * const * cannot be assigned to char const * const *
2011-07-14 16:09 [Bug c/49748] New: char * const * cannot be assigned to char const * const * gcc at misc dot lka.org.lu
@ 2011-07-14 16:44 ` redi at gcc dot gnu.org
2011-07-14 17:39 ` gcc at misc dot lka.org.lu
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2011-07-14 16:44 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49748
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-07-14 16:44:08 UTC ---
the bug reporting guidelines clearly ask for a complete and self-contained test
case, not several snippets of code which don't form a complete program
http://gcc.gnu.org/bugs/
the warning is correct, see http://c-faq.com/ansi/constmismatch.html
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/49748] char * const * cannot be assigned to char const * const *
2011-07-14 16:09 [Bug c/49748] New: char * const * cannot be assigned to char const * const * gcc at misc dot lka.org.lu
2011-07-14 16:44 ` [Bug c/49748] " redi at gcc dot gnu.org
@ 2011-07-14 17:39 ` gcc at misc dot lka.org.lu
2012-02-23 14:13 ` tim.ruehsen at gmx dot de
2015-02-26 19:44 ` mpolacek at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: gcc at misc dot lka.org.lu @ 2011-07-14 17:39 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49748
--- Comment #2 from Alain Knaff <gcc at misc dot lka.org.lu> 2011-07-14 17:38:59 UTC ---
Created attachment 24756
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=24756
Test program for const warning bug
> the warning is correct, see http://c-faq.com/ansi/constmismatch.html
Thanks for this link. This does indeed confirm what I said, in much less words
than I needed. Moreover, it points out that the issue doesn't exist in C++.
However, it doesn't explain why the issue still present in C.
Any insights into this question?
If this is just to keep some paper-pushers happy, maybe we could keep the
current behavior as the default, but have a -W flag to make gcc mimic g++'s
behavior?
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/49748] char * const * cannot be assigned to char const * const *
2011-07-14 16:09 [Bug c/49748] New: char * const * cannot be assigned to char const * const * gcc at misc dot lka.org.lu
2011-07-14 16:44 ` [Bug c/49748] " redi at gcc dot gnu.org
2011-07-14 17:39 ` gcc at misc dot lka.org.lu
@ 2012-02-23 14:13 ` tim.ruehsen at gmx dot de
2015-02-26 19:44 ` mpolacek at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: tim.ruehsen at gmx dot de @ 2012-02-23 14:13 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49748
Tim Ruehsen <tim.ruehsen at gmx dot de> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |tim.ruehsen at gmx dot de
--- Comment #3 from Tim Ruehsen <tim.ruehsen at gmx dot de> 2012-02-23 14:12:00 UTC ---
Just to clarify it and to add a complete program.
#### x.c ####
void f(const char *const *args) {}
int main(int argc, char **argv) {
f(argv);
return 0;
}
####
Compiling with g++ (4.6.2) will do without warning.
Compiling with gcc (4.6.2) gives:
x.c: In function 'main':
x.c:4:2: warning: passing argument 1 of 'f' from incompatible pointer type
[enabled by default]
x.c:1:6: note: expected 'const char * const*' but argument is of type 'char **'
This is somewhat annoying when trying to harden older C sources with
-Wwrite-strings.
One has to insert (very) many casts to avoid the above warning.
This is much work that could be avoided by an apropriate -W option.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/49748] char * const * cannot be assigned to char const * const *
2011-07-14 16:09 [Bug c/49748] New: char * const * cannot be assigned to char const * const * gcc at misc dot lka.org.lu
` (2 preceding siblings ...)
2012-02-23 14:13 ` tim.ruehsen at gmx dot de
@ 2015-02-26 19:44 ` mpolacek at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-02-26 19:44 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49748
Marek Polacek <mpolacek at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
CC| |mpolacek at gcc dot gnu.org
Resolution|--- |FIXED
--- Comment #4 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
(In reply to Tim Ruehsen from comment #3)
> This is much work that could be avoided by an apropriate -W option.
GCC 5 has now the -Wincompatible-pointer-types option that can be used to quiet
the warning.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2015-02-26 18:22 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-14 16:09 [Bug c/49748] New: char * const * cannot be assigned to char const * const * gcc at misc dot lka.org.lu
2011-07-14 16:44 ` [Bug c/49748] " redi at gcc dot gnu.org
2011-07-14 17:39 ` gcc at misc dot lka.org.lu
2012-02-23 14:13 ` tim.ruehsen at gmx dot de
2015-02-26 19:44 ` mpolacek at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).