0
    #include<stdio.h>
    int main(){
       char *ptr="Helio";
       ptr++;
       printf("%s\n",ptr);
       //*ptr++;
       printf("%c\n",++*ptr);/*Segmentation fault on GCC*/
       return 0;
    }

Q1) This works fine in Turbo C++ but on GCC it gives segmentation fault. I am not getting the exact reason.

May be operator precedence is one of the reason.

Q2) Do each compiler has different operator precedence?

As I can see here ++ has higher precedence than dereference operator. May be GCC and Turbo C++ treats them differently.

M.M
  • 138,810
  • 21
  • 208
  • 365
anuj pradhan
  • 2,777
  • 4
  • 26
  • 31
  • no this is because you are pointing to a constant string but you dereference the first character – v.oddou Jul 15 '14 at 05:02
  • you want to increment ptr or value in ptr[0]? – mohitj2007 Jul 15 '14 at 05:02
  • Operator precedence could not be an issue... `(++*)ptr` is not valid syntax! – M.M Jul 15 '14 at 05:13
  • 1
    @MattMcNabb but it would make a difference in the commented out line, without parens it's equivalent to `*(ptr++)`. Perhaps that's what's confusing, the behavior between the two forms is so completely different. – Mark Ransom Jul 15 '14 at 05:21

3 Answers3

5

No, the operator precedence is defined by the C standard, all the compiler follows the same one.

The reason of difference result of Turbo C++ and GCC in this case is because you modified the string literal, which is undefined behavior.

Change it to:

char arr[] = "Helio";
char *ptr = arr;

and you can modify the content of the string now. Note that arr itself is the array name and cannot be modified, so I added a new pointer variable ptr and initialize it to point to the first element of the array.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
4

In your last printf() line, the expression ++*ptr is equivalent to ++ptr[0], which is, in turn, equivalent to ptr[0] = ptr[0]+1. Since ptr[0]=='H', you are trying to change the value of ptr[0] to 'I'.

That's the key problem there. Since &ptr[0] points to the first element of the constant "Helio", the attempt to change the first character, H, is giving trouble, because it is Undefined Behaviour.

pablo1977
  • 4,281
  • 1
  • 15
  • 41
  • @v.oddou +1 this is good attempt to the right answer :).No down vote suet for it. – Jayesh Bhoi Jul 15 '14 at 05:40
  • @v.oddou: My answer is a very easy and very standard answer, so it has not a really "owner". There is not anything to plagiarize. Your answer start with wrongs concepts. On the other hand Yu Hao is not completely clear in the central issue. So, I decided to give a clear and simple answer. Jayesh well undertood the situation. – pablo1977 Jul 15 '14 at 05:45
  • @pablo1977: I edited it, so the "start with wrong concepts" part, no longer applies. And to my mind, never had, but that's just me. You're right about all the rest. But please understand that I feel frankly bitter by the fact that people simply decided to blindly follow quantdev remark, and virtually made my answer invisible now, when in actuality it states more than yours, and you already have an upvote. where is that fair – v.oddou Jul 15 '14 at 05:51
  • @v.oddou why not fair to up-vote the good answer? anyone not force to anyone to use syntax like this or like this.Let leave it on compiler it will do best. Any way your attempt is good and appreciated all comments. keep it up.+1 for you when you some make edition in it. – Jayesh Bhoi Jul 15 '14 at 06:02
  • @Jayesh: thank you :) I appreciate. I did edit a lot to get everybody happy. – v.oddou Jul 15 '14 at 06:16
  • @v.oddou For one thing, this answer does not waste space with a discussion of “C-people” and their smelly ideas. So that's not plagiarism in my opinion. Conversely, you could take this answer as an inspiration of what a SO answer is supposed to look like. – Pascal Cuoq Jul 15 '14 at 07:35
2
char* p = "some literal";

This is only legal because of a smelly argument that C-people fought over during standard comitee negociations. You should consider it as an oddity that exists for backward compatibility.

This is the message you get with GCC:

warning: deprecated conversion from string constant to 'char*'

Please next time, write the following:

char const* p = "some literal";

And make it a reflex in your coding habits. Then you would not have been able to compile your faulty line.

which is:

++*ptr

Here you are taking the first character of the constant literal and try to increment it, to what comes after H, therefore I. But this memory zone happens to be in a write protected page, because this is a constant. This is very much undefined by standard and you should consider it illegal. Your segfault comes from here. I suggest you run your program in valgrind next time to get more elaborate error messages.

In the answer that Yu Hao wrote for you, what is happenning is that all the characters gets copied one by one, from the constant string pool where the literal are stored, to a stack-allocated char array, by a code that the compiler writes at the initialization/declaration site, therefore you can dereference its content.

v.oddou
  • 6,476
  • 3
  • 32
  • 63
  • 5
    Its not an illegal statement – quantdev Jul 15 '14 at 05:08
  • you should consider like it is. because you const cast from const to non const. as long as you access only in read, its ok, but access in write and boom. some compilers give build errors on this kind of declaration. – v.oddou Jul 15 '14 at 05:10
  • more in-depth here : http://stackoverflow.com/questions/3075049/why-do-compilers-allow-string-literals-not-to-be-const – v.oddou Jul 15 '14 at 05:14
  • @v.oddou every thing is right like to modify string literal is illegal because it's in read only part. But it's declaration not illegal . it just created a pointer and pointed it at a constant string. So correct that first. – Jayesh Bhoi Jul 15 '14 at 05:25
  • @Jayesh: thanks, I see your point and I edited the controversial line in order to suit the pedantic moods of the community. On the other hand I made my expression very clear, using an extended phrase of what I was conveying using the previous formulation that got me downvoted. I feel pretty beffudled by this downvote spree actually, I have the feeling it just takes one person to itch a pedantic part of terminology to get a string of other people going "oh its downvote party here, yay !" ... well... – v.oddou Jul 15 '14 at 05:30
  • 2
    I did not downvote, but feel like choosing the exact wording is very important, not pedantic, and the only way to go on a QA site like StackOverflow. A beginner could easily have read you as "it's illegal! It should not compile! so why my compiler is compiling this ?!". Being demanding is good for everybody. – quantdev Jul 15 '14 at 05:42
  • @quantdev But it is deprecated in C++. So it is on its way to being illegal. A recent C++ compiler should give you a warning. – juanchopanza Jul 15 '14 at 06:03
  • @juanchopanza: yes, I edited the answer to emphasize that fact :) – v.oddou Jul 15 '14 at 06:14
  • @v.oddou +1. Now it's looks bunch of appropriate details. – Jayesh Bhoi Jul 15 '14 at 06:16
  • @Jayesh: this is thanks to the community criticism. We could say that it forced me to state everything in clean shape. However I will remember the harsh way in which it did, and refrain from helping people in the future. – v.oddou Jul 15 '14 at 06:18