LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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: 15

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: 399

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,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
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: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
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 Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
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: 399

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,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
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 Veteran
 
Registered: May 2008
Posts: 6,904

Rep: Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025Reputation: 5025
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: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
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: 998

Rep: Reputation: 471Reputation: 471Reputation: 471Reputation: 471Reputation: 471
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,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
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: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
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: 309

Rep: Reputation: 92
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 Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
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



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 06:26 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