HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
I am curious to know just what you mean by "run on your compiler". Your code compiles on the MinGW port of gcc 3.4.5 with just one warning (you forgot to #include <string.h>), but it is logically incorrect since it attempts to modify a string literal, as that part of the OP's code was not fixed. My guess is that you (incorrectly) interpreted "even the code you gave is not running" as "even the code you gave does not compile". If not, and you actually ran the program and it worked, then it is just an effect of undefined behaviour - that it compiles is no guarantee of correctness.Originally Posted by Adak
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I mean the compiler is able to run through it's work, and create an executable file, without warnings or errors. Turbo C has a smart compiler which is able to include strlen() functionality, into a program, despite not explicitly including string.h.
The resulting program runs correctly. Some sample output appropriate for July 4th:
Hoorah for the flag of the free!
May it wave as our standard forever
The gem of the land and the sea
The banner of the right.
Let tyrants remember the day
when our fathers with mighty endeavor,
proclaimed as they marched to the quay
that by their might and by their right
it waves forever...
!eerf eht fo galf eht rof harooH
reverof dradnats ruo sa evaw ti yaM
aes eht dna dnal eht fo meg ehT
.thgir eht fo rennab ehT
yad eht rebmemer stnaryt teL
,rovaedne ythgim htiw srehtaf ruo nehw
yauq eht ot dehcram yeht sa demialcorp
thgir rieht yb dna thgim rieht yb taht
...reverof sevaw ti
Press Enter When Ready
Now after searching the net I get to know that string literals can't be modified as they ar stored in read-only section of the memory by the OS. But my question is why string literals are given such type of a special "treatment"?
And by the way is anything between double quotes a string literal?
Maybe the last line of this FAQ is helpful here.
C-FAQ
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
After going through the details of arrays of pointers and character strings(and also hijacking your thread) I made 3 solutions of this question. But in all of them the main thing is that I'm swapping the characters of each string until the mid-character. Out of the three approaches I took one of them was with malloc which is similar to your code posted. But I dont understand what you're trying to do in the colored lines. What I've done is after assigning the storage to p, copied a into p and then the same swapping thing. This way the swapped string gets stored in p.
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
That is, the compiler was able to compile the code without warnings or errors. That is different from saying that the program does not run (but admittedly I see the potential for confusion since a program that does not compile obviously does not run: perhaps BEN10 should have mentioned a run time error or something).Originally Posted by Adak
Haha. That is not the sign of a "smart compiler", since a compiler (or linker/loader invoked by the compiler) might link to the C standard library by default, and it is possible for a function to be used without being declared at the point of usage. One possible reason that you did not get a warning is that <string.h> could have been included by a header that you did include - relying on this would be relying on something implementation specific. Another possible reason is that the warning level was not high enough.Originally Posted by Adak
As such, you are observing the effect of undefined behaviour. It appears to work in your case, but is nonetheless incorrect.Originally Posted by Adak
I believe there are practical reasons related to how implementations might implement string literals, but consider this: what does it mean to modify a literal? For example, does this make sense to you?Originally Posted by BEN10
Code:1 = 2;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks everybody. Now I think some of the loops in my mind are opening regarding pointers.
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition