0
#include<stdio.h>

int main(void)
{
    const int a = 6;
    int* const ptr = &a;

    *ptr = a*a;
    printf(" a= %d ptr = %d ", a, ++ *ptr);
    printf(" a= %d ptr = %d ", a, -- *ptr);
    return 0;
}

Above is the program I'm referring to.
For me the output, I'm getting is no. 1)

  1. a = 6 ptr = 37 a= 6 ptr = 36
  2. a = 37, ptr = 37 a = 36, ptr = 36

If I'm using an online editor I'm getting output no. 2). My instructor too showed me the output as no. 2), it is a windows system and has MingW installed in it. How is that so ? I decided to go for MacBook Pro for programming after many research. Also through pen and paper the output is 2). What changes I need to make to this code to get the same output as no. 2) ?

Can anyone please help me with this ? Also what other precautions I need to take while using Mac, if MingW is not installed ?

Thanks in advance.

Tried to run on Mac using Visual basic IDE however the outputs on Mac and Windows are different.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • Please pay attention to any warnings your compiler generates. – G.M. Mar 21 '23 at 15:16
  • 3
    Assigning to `*ptr` tries to modify a `const` variable. This causes undefined behavior. – Barmar Mar 21 '23 at 15:19
  • 1
    And even if it weren't const, using `a` and `++ *ptr` in the same argument list is also undefined behavior. It's the same as using both `a` and `++a`. – Barmar Mar 21 '23 at 15:22
  • Don't waste your time with obviously fishy code like this. What exactly did your teacher/instructor/tutor or whatever tell you about this code? – Jabberwocky Mar 21 '23 at 15:26
  • The platform (Mac, Windows, whatever online compiler) etc. doesn't matter here. Your code has _undefined behaviour_ (google that term). – Jabberwocky Mar 21 '23 at 15:30
  • 2
    Well crap code or not, it is good that students bring such questions here instead of trying to find the (non-existing) answer. Challenging the (in)competence of the teacher is a tough job and without formal sources it's probably not going to work. – Lundin Mar 21 '23 at 15:42
  • I understand that we modify the value of the constant variable using the pointer, however the code is not giving me any error. I'm just concern that the code is giving some other output on a windows system and different on Mac system. – Rutuja Bhairawakar Mar 22 '23 at 16:05
  • @RutujaBhairawakar invalid code does not necessarily give an error. What you're seeing here is called _undefined behaviour_ (google that term). Anyway it's not only about modifying a const variable with a pointer, there are more issues, all of which are explained in great details in [this answer](https://stackoverflow.com/a/75803147/898348) – Jabberwocky Mar 22 '23 at 16:24
  • @RutujaBhairawakar I'd be curious what your instructor/teacher/tutor or whatever has to say about this piece of code. Please report here and let us know. Also let us know where this code comes from (a book, a website, your lecture notes,...)- – Jabberwocky Mar 22 '23 at 16:26
  • _"Also what other precautions I need to take...."_: don't write unreadable pointless crap code like this. – Jabberwocky Mar 22 '23 at 16:30

3 Answers3

3

The code is invalid C and also invokes undefined behavior in multiple ways. Meaning there is no predictable outcome and the program might as well crash or the compiler could generate strange machine code. There is no "correct" or "valid" answer to be had.

  • Issue #1, a, ++ *ptr and a, -- *ptr. It is undefined behavior because of C17 6.5

    If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.

    The object a is modified through ++*ptr in a function argument list, unsequenced in relation to the value computation of that same object.

  • Issue #2, int* const ptr = &a; is invalid C, since it is not one of the valid forms listed in C17 6.5.16.1. In plain English, you cannot assign a const int* to a int* const (or to a int* for that matter).

  • Issue #3 *ptr = ... is undefined behavior since the location is read-only. C17 6.7.3:

    If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 2
    Your instructor should know these things. This code is severely broken and they should tell you as much, instead of telling you to "look for the correct answer". There is no "correct answer". If the code comes from your instructor then they are in need to _take_ a C beginner class, not host one. – Lundin Mar 21 '23 at 15:24
1

This answer is correct, but it might be somewhat hard to understand for beginners.

Let's simplify somewhat and let's see what happens with these lines of code:

int a = 6;
int* ptr = &a;
printf(" a= %d ptr = %d ", a, ++ *ptr);

ptr points to a, therefore writing *ptr is more or less the same as writing a. So printf(" a= %d ptr = %d ", a, ++ *ptr) is basically the same as:

printf(" a= %d ptr = %d ", a, ++a);

Now the order of evaluation the of arguments you pass to a function is not determined in the C language. Your code might evaluate first a and then ++a, or first ++a and then a, you don't know which alternative your compiler chooses.

The outcome is of course different in both cases.

I suggest you also read this: Why are these constructs using pre and post-increment undefined behavior?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
0

You have an issue with your code.

a is a constant integer.

When you assign it with the new value via the pointer your brake your own promise given to the compiler and attempt to modify const object invokes undefined behaviour.

Compiler is basically free to do what it wants and the programs behaviour cannot be predicted from this point.

0___________
  • 60,014
  • 4
  • 34
  • 74