4
#include <stdio.h>

int main() 
{
    const int marbles[10] = { 1,2,3,4,5,6,7,8,9,10 };
    int *ptr = marbles;
    *ptr = 100;
    printf("%d \n", marbles[0]); // output is "100"

}

I used const to protect the array. So I thought int *ptr = marbles would cause error. Otherwise by using pointer, it would enable user to change the data in the array. But surprisingly the output is "100". Isn't C suppose to protect the array from any kind of methods when I use const?

Jin
  • 1,902
  • 3
  • 15
  • 26
  • Isn't it enough that the compiler utters a warning? – deamentiaemundi Aug 08 '16 at 04:09
  • `int *ptr = marbles;` broke the contract. Change to `const int *ptr = marbles;` – chux - Reinstate Monica Aug 08 '16 at 04:10
  • the assignment of `marbles` to `ptr` is an incompatible pointer assignment as it discards the `const` qualifier and violates the C standard, but most compilers issue a warning using default settings to avoid breaking legacy builds – obataku Aug 08 '16 at 04:10
  • 1
    C does not protect you from mistakes, it just manages the best it could. Compile the code with warnings enabled to enable the errors. I think the correct warning is `-Wno-pointer-sign` – smac89 Aug 08 '16 at 04:10
  • 2
    GCC complains (warning or error) about: `error: initialization discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]` for the line `int *ptr = marbles;` — so it doesn't compile cleanly. Pay heed to your compiler. If it didn't warn you, turn up the warning levels, or get a better compiler. – Jonathan Leffler Aug 08 '16 at 04:11
  • @deamentiaemundi Oh I see.. But I expected an error message, not a warning message. That's what confused me! – Jin Aug 08 '16 at 04:11
  • @JonathanLeffler I'm using visual studio 2015. Should I get another compiler? – Jin Aug 08 '16 at 04:13
  • @jwqwerty: I decline to answer that question — this is meant to be a family show. What warning level were you using? ISTR `/W3` or thereabouts being fairly fussy; was there a `/W4`? – Jonathan Leffler Aug 08 '16 at 04:14
  • @jwqwerty visual studio has a **very** poor support of the C standard, to put it politely, and they do not plan to change that. – deamentiaemundi Aug 08 '16 at 04:16
  • @JonathanLeffler It gave me warning C4090. Guess that's /W4? – Jin Aug 08 '16 at 04:19
  • Pass: I've not used a Visual Studio compiler in anguish in the current decade — I am not au fait with its current error messages. I am mildly amazed I remember the `/Wn` options. – Jonathan Leffler Aug 08 '16 at 04:20
  • @deamentiaemundi I should use another compiler! BTW which compiler are you using? – Jin Aug 08 '16 at 04:21
  • Note that if the array is global, then you probably get a runtime error. The initialized data is probably placed in read-only memory (as part of the text segment, in Unix parlance), and the attempted assignment leads to a crash. This was tested on Mac OS X; I can't be sure what would happen with MSVC. The local (stack-based) array can't readily be given that much protection. – Jonathan Leffler Aug 08 '16 at 04:24
  • @deamentiaemundi: Really? Last time I checked Visual Studio had excellent support for C standard and they were rapidly improving it from version to version. Which is exact opposite of your claim. So what are you talking about? – AnT stands with Russia Aug 08 '16 at 04:40
  • @AnT Now I'm confused... – Jin Aug 08 '16 at 04:45
  • @jwqwerty: Why? Traditionally, C compilers issue "a mere warning" for this kind of violation (all of them, not just Visual Studio). You just need to understand that "warning" can easily stand for an "error". – AnT stands with Russia Aug 08 '16 at 04:47
  • @AnT they aim to support C90 but [that's it](https://herbsutter.com/2012/05/03/reader-qa-what-about-vc-and-c99/). "*the C standard*" is a little vague, so maybe he means the modern standards. – Ryan Haining Aug 08 '16 at 04:47
  • 1
    @Ryan Haining: Absolutely wrong. The page you linked became obsolete many years ago. Visual Studio committed to C99 support several versions ago, and by now they have already implemented full support for C99 (with few minor exceptions, like [now optional] VLA and slightly non-compliant `restrict`,although I haven't tested the latest update). – AnT stands with Russia Aug 08 '16 at 04:48
  • @AnT Okay. So I guess it's safe to use visual studio 2015? – Jin Aug 08 '16 at 04:49
  • @AnT source? I'm sure they added whatever was new to C++11/14 to their C mode but I can't find anything they have C99 support. – Ryan Haining Aug 08 '16 at 04:51
  • 1
    @Ryan Haining: http://stackoverflow.com/questions/27826409/what-is-the-official-status-of-c99-support-in-vs2013. Another source: just try it. – AnT stands with Russia Aug 08 '16 at 04:52
  • @AnT [splendid!](https://blogs.msdn.microsoft.com/vcblog/2013/06/28/c1114-stl-features-fixes-and-breaking-changes-in-vs-2013/) I don't have any windows computers in my life anymore with which to try it, just online compilers. – Ryan Haining Aug 08 '16 at 04:59
  • @AnT Yep, VLA is not working on latest version of Visual Studio 2015. BTW do you know whether VS is planning to support VLA? – Jin Aug 08 '16 at 06:34
  • @AnT if "standard" is not specified it's the current one and the current one is ISO/IEC 9899-2011, a five year old standard. If such a large IT-company like Microsoft does not support a current IT-standard after 5 years we can safely assume that they do not want to do so in the near future, if at all. That's a business decision. I don't have MS-shares, so I don't care. There are several free C-compilers for Windows and if you develop for Intel hardware you can use their compiler (I think they have a free version, too, but I'm not sure) which has commercial support. – deamentiaemundi Aug 08 '16 at 12:52

2 Answers2

3

When I tried to compile the program, it showed this warning. So I guess when you initialize the pointer, const keyword loses its effect.

In function ‘main’:
try.c:6:16: warning: **initialization discards ‘const’ qualifier from pointer** target type [enabled by default]
     int *ptr = marbles;
taskinoor
  • 45,586
  • 12
  • 116
  • 142
2

So I thought int *ptr = marbles would cause error.

It is an "error". C language considers this initialization a constraint violation, which is what we typically call an "error".

The compiler is required to issue a diagnostic message in response to a constraint violation, which is exactly what it did in your case.

Typically, it is your responsibility to analyze diagnostic message issued by your compiler and figure out which are "innocent" and which indicate serious errors.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765