2

I have a question regarding the following code:

#include <stdio.h>


int main() {
char days[7][10] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
char *pdays[7]= {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

(void) printf("%d\n", days[4] - days[1]);
(void) printf("%d\n", pdays[4] - pdays[1]);
}

If I run it in CLion (Running on OSX MacBook Pro (13-inch, Mid 2012), Processor: 2.5 GHz Intel Core i5, Memory: 4 GB 1600 MHz DDR3) returns

30
8

If I run it in http://www.tutorialspoint.com/compile_c_online.php it returns

30
27

What gives? I'm studying for my ProgC Exam and I don't know what to trust now. Any help would be greatly appreciated.

dandan78
  • 13,328
  • 13
  • 64
  • 78
Daniel Einars
  • 133
  • 14
  • [Sourave Gosh](http://stackoverflow.com/users/2173917/sourav-ghosh) , Could you show me the duplicate as I have been unable to find it. – Daniel Einars Jun 14 '16 at 10:39
  • Daniel Einars, I would say that the 30 - 27 looks the most correct, I am a bit baffled about the 30 - 8, which compiler do you use with CLion, and what settings? – Tommy Andersen Jun 14 '16 at 10:52
  • 1
    Actually to correct my previous statement, both can actually be correct, since the compiler can choose not to give unreferenced variables an address. So even though you have defined Wednesday and Thursday, they haven't necessarily gotten an address. That is, until you use them. :) – Tommy Andersen Jun 14 '16 at 10:58
  • I agree this is not a duplicate, since the "problem" here is due to compiler optimzation, and not simple pointer arithmetics – Tommy Andersen Jun 14 '16 at 10:59
  • 1
    Try changing your example to this: http://ideone.com/fkNzzx and rerun in CLion :) – Tommy Andersen Jun 14 '16 at 11:06
  • 1
    Thank you [Tommy Andersion](http://stackoverflow.com/users/111143/tommy-andersen) for your help. When I run your code from ideone it returns the "correct" values. As you suggested I looked into the compiler settings (Build|Execution|Deployment -> Make) and added the option "-D CMAKE_C_COMPILER=GCC" according to the [JetBrains CLion FAQ](https://blog.jetbrains.com/clion/2014/09/clion-answers-frequently-asked-questions/). After this change the original code returns the expected values. It seems CLion uses a different compiler when using the default installation. Again, thank you for your help! – Daniel Einars Jun 14 '16 at 11:23
  • I don't think this is a duplicate. – babon Jun 14 '16 at 11:41
  • Stackoverflow is not a forum. Do not add things like [solved] to the title of your question. – dandan78 Jun 14 '16 at 11:51
  • well I'd very much like to mark it as answered but my reputation won't let me a) quote the comment which is the answer and mark it as answered and b) mark a comment as the answer. :( – Daniel Einars Jun 14 '16 at 11:53
  • 1
    When you are doing `printf("%d\n", pdays[4] - pdays[1]);`, you are basically substracting a pointer (unsigned int / long) from another. The pointer addresses stored at `pdays[4]` and `pdays[1]` can be anything. – babon Jun 14 '16 at 11:55

0 Answers0