ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
On running this simple C code, it shows a warning 'initialization discards ‘const’ qualifier from pointer target type' and modifies the const var which I thought shouln't be modified. Could someone please explain?
#include <stdio.h>
#include<stdlib.h>
int main()
{
const int var=10;
int * ptr = &var;
printf(" %d \n", *ptr);
*ptr = 20;
Your compiler warned you and you chose to ignore the warning and proceed any way. C allows that. It has a weak type system and allows the programmer a lot of leeway.
If your compiler decides to do some agressive optimalization, it might change the storage class of `var` from `auto` to `static`. Also it might put this `static const` variable into a read-only segment, so your program will abort.
that is just false. You can use any tool to make good things and bad things (including a hammer, a smartphone or a compiler).
Without really knowing your tool [c language] you will [sooner or later] fail.
C will not restrict you to do anything you wish. Other languages do not allow to handle memory, which will lead to the next problem called garbage collector.
But this is completely offtopic here.
that is just false. You can use any tool to make good things and bad things (including a hammer, a smartphone or a compiler).
Without really knowing your tool [c language] you will [sooner or later] fail.
C will not restrict you to do anything you wish. Other languages do not allow to handle memory, which will lead to the next problem called garbage collector.
But this is completely offtopic here.
I'm not sure I'd dismiss the article out of hand. If memory serves, Peter van der Linden made related comments many years ago (in "Deep C Secrets"). I think it The standards committee tends to err on the side of "do nothing". I think he questioned why the function gets would be left to continue wreaking havoc. You could say removing it would break code or you could say it would reveal broken code.
C will not only give you the rope, it will give you the chair to climb up on!
$ cc -Wall huh.c
huh.c: In function 'main':
huh.c:4:13: warning: self-comparison always evaluates to true [-Wtautological-compare]
if (&main == *main) {
^~
$
C may let you hang yourself, but it usually puts a sign up right in front of the noose saying "warning: proximity to loop with running knot." To me, that's as it should be: though often the wording of the warnings are a little too technical and could be clearer.
I find warnings usually catch it whenever I do something dumb.
initialization discards ‘const’ qualifier from pointer target type' and modifies the const var which I thought shouln't be modified. Could someone please explain?
The issue is that the compiler allows you to try and do that?
To make it clear, this is the exact message (gcc 4.8.5):
Code:
a.c: In function ‘main’:
a.c:8:13: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default]
int * ptr = &var;
^
The meaning: var is initially a const, but ptr is not. Theoretically it is not a problem at all, because any pointer (like ptr) can point to anywhere, including functions, consts or the outer space.
But from this point the compiler cannot take care about that const (var) any more, because it can be freely modified via ptr - the const qualifier is lost.
You can use different compiler flags to detect these kind of constructs and also to disable them - drop an error message if found.
these qualifiers were initially handled during compilation, because later, during execution there was no any way to check if a given memory was declared as const in the source file. Nowadays it is much more complicated, but it is still valid: these qualifiers are used to restrict the developers (human beeings) to avoid the misuse of fixed/hardcoded values.
Remember, you never fool/confuse a computer but yourself.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.