LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-10-2020, 05:17 PM   #1
atharvdesai1996
LQ Newbie
 
Registered: Dec 2019
Posts: 13

Rep: Reputation: Disabled
const variable modified by a C pointer


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;

printf(" %d \n", *ptr);

}
 
Old 08-10-2020, 05:27 PM   #2
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 325

Rep: Reputation: Disabled
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.
 
2 members found this post helpful.
Old 08-10-2020, 05:36 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,075

Rep: Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475
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.
 
2 members found this post helpful.
Old 08-10-2020, 06:00 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,918
Blog Entries: 13

Rep: Reputation: 4043Reputation: 4043Reputation: 4043Reputation: 4043Reputation: 4043Reputation: 4043Reputation: 4043Reputation: 4043Reputation: 4043Reputation: 4043Reputation: 4043
Besides the correct answers already given, perhaps read the compiler documentation, or also look up the C const compiler directive.

I'm sure there are other references, but this seems to cover it
 
1 members found this post helpful.
Old 08-10-2020, 11:12 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,774

Rep: Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409
More broadly:

Why the C Language Will Never Stop You from Making Mistakes
 
1 members found this post helpful.
Old 08-11-2020, 05:43 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 15,130

Rep: Reputation: 4982Reputation: 4982Reputation: 4982Reputation: 4982Reputation: 4982Reputation: 4982Reputation: 4982Reputation: 4982Reputation: 4982Reputation: 4982Reputation: 4982
Quote:
Originally Posted by dugan View Post
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.
 
Old 08-11-2020, 07:39 AM   #7
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 325

Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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!
 
Old 08-11-2020, 08:38 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,075

Rep: Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475
While is it off-topic here, no-one can say C is spotless. A little example:
Code:
#include <stdio.h>

int main (void) {
  if (&main == *main) {
    printf ("Huh?\n");
  }
  return 0;
}
 
Old 08-11-2020, 09:23 AM   #9
GazL
LQ Guru
 
Registered: May 2008
Distribution: CRUX
Posts: 5,548
Blog Entries: 14

Rep: Reputation: 3394Reputation: 3394Reputation: 3394Reputation: 3394Reputation: 3394Reputation: 3394Reputation: 3394Reputation: 3394Reputation: 3394Reputation: 3394Reputation: 3394
And yet,
Code:
$ 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.
 
1 members found this post helpful.
Old 08-11-2020, 10:53 AM   #10
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,774

Rep: Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409
I find that clang-tidy helps.
 
Old 08-11-2020, 12:41 PM   #11
EdGr
Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 483

Rep: Reputation: 234Reputation: 234Reputation: 234
OP - If you want real constants, use C++. C++ can usually compile C programs that adhere to the stricter semantics.
Ed
 
Old 08-11-2020, 01:02 PM   #12
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,075

Rep: Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475
Speaking of C++, you might want to learn about `const_cast` too.
 
Old 08-11-2020, 01:37 PM   #13
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,774

Rep: Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409Reputation: 4409
Quote:
Originally Posted by NevemTeve View Post
Speaking of C++, you might want to learn about `const_cast` too.
And constexpr, while we're on the topic of C++'s true constants.
 
Old 09-01-2020, 01:07 AM   #14
Fat_Elvis
Member
 
Registered: Oct 2016
Distribution: FreeDOS 1.2
Posts: 299

Rep: Reputation: 90
Quote:
Originally Posted by atharvdesai1996 View Post
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?
 
Old 09-01-2020, 01:48 AM   #15
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 15,130

Rep: Reputation: 4982Reputation: 4982Reputation: 4982Reputation: 4982Reputation: 4982Reputation: 4982Reputation: 4982Reputation: 4982Reputation: 4982Reputation: 4982Reputation: 4982
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.
 
  


Reply

Tags
c programming, constant, pointers


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
In c using local variable as seperate functions or a global variable for a const Alpha90 Programming 4 06-15-2015 06:04 AM
how to assign a “const void*” to a “const uint64_t*” in cuda c? arungpillai09054 Programming 2 04-25-2013 06:14 AM
Convert const wchar_t* to const char* to use in fprintf() coders123 Programming 1 01-17-2011 03:47 PM
any difference between "const myClass &obj" and "myClass const &obj"? parv Programming 9 01-09-2008 08:58 AM
diff between #define and const defined variable b123coder Programming 7 06-27-2005 08:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration