Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why??

Why??

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    #include <iostream.h> #include <string.h> void main() { char *pstr="test" ; cout<<strrev(pstr)<<endl; } This program isn't executed in VC++6.0, but in C++ Builder5.0, Why?

    F R 2 Replies Last reply
    0
    • L Lost User

      #include <iostream.h> #include <string.h> void main() { char *pstr="test" ; cout<<strrev(pstr)<<endl; } This program isn't executed in VC++6.0, but in C++ Builder5.0, Why?

      F Offline
      F Offline
      Frank Liao
      wrote on last edited by
      #2

      #include <iostream.h> #include <string.h> void main() { char *pstr="test" ; cout<<strrev(pstr)<<endl; } OK, then Borland is not comforming to C. You wrote code that is attempting to modify a constant string. _strrev actually modifies the character array/pointer, so an error occurs here because you are modifying a constant. The correct way of doing this problem would be: #include <iostream.h> #include <string.h> void main() { char str[] = "test"; cout << _strrev(str) << endl; } Frank

      L 1 Reply Last reply
      0
      • F Frank Liao

        #include <iostream.h> #include <string.h> void main() { char *pstr="test" ; cout<<strrev(pstr)<<endl; } OK, then Borland is not comforming to C. You wrote code that is attempting to modify a constant string. _strrev actually modifies the character array/pointer, so an error occurs here because you are modifying a constant. The correct way of doing this problem would be: #include <iostream.h> #include <string.h> void main() { char str[] = "test"; cout << _strrev(str) << endl; } Frank

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Thanks, Frank.
        There is another question .
        The program taken from an article "A Sample Generic-Text Program" from MSDN isn't executed in VC++ 6.0 , Whether it modifies a constant string either?
        #include <stdio.h > #include <stdlib.h > #include <string.h > #include <direct.h > #include <errno.h > #include <tchar.h > int __cdecl _tmain(int argc, _TCHAR **argv, _TCHAR **envp) { _TCHAR buff[_MAX_PATH]; _TCHAR *str = _T("Astring"); char *amsg = "Reversed"; wchar_t *wmsg = L"Is"; #ifdef _UNICODE printf("Unicode version\n"); #else /* _UNICODE */ #ifdef _MBCS printf("MBCS version\n"); #else printf("SBCS version\n"); #endif #endif /* _UNICODE */ if (_tgetcwd(buff, _MAX_PATH) == NULL) printf("Can't Get Current Directory - errno=%d\n", errno); else _tprintf(_T("Current Directory is '%s'\n"), buff); _tprintf(_T("'%s' %hs %ls:\n"), str, amsg, wmsg); _tprintf(_T("'%s'\n"), _tcsrev(str)); return 0; }

        M F 2 Replies Last reply
        0
        • L Lost User

          Thanks, Frank.
          There is another question .
          The program taken from an article "A Sample Generic-Text Program" from MSDN isn't executed in VC++ 6.0 , Whether it modifies a constant string either?
          #include <stdio.h > #include <stdlib.h > #include <string.h > #include <direct.h > #include <errno.h > #include <tchar.h > int __cdecl _tmain(int argc, _TCHAR **argv, _TCHAR **envp) { _TCHAR buff[_MAX_PATH]; _TCHAR *str = _T("Astring"); char *amsg = "Reversed"; wchar_t *wmsg = L"Is"; #ifdef _UNICODE printf("Unicode version\n"); #else /* _UNICODE */ #ifdef _MBCS printf("MBCS version\n"); #else printf("SBCS version\n"); #endif #endif /* _UNICODE */ if (_tgetcwd(buff, _MAX_PATH) == NULL) printf("Can't Get Current Directory - errno=%d\n", errno); else _tprintf(_T("Current Directory is '%s'\n"), buff); _tprintf(_T("'%s' %hs %ls:\n"), str, amsg, wmsg); _tprintf(_T("'%s'\n"), _tcsrev(str)); return 0; }

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          Because when you declare: TCHAR* str = "blah blah"; the literal string "blah blah" is stored in the executable. When loaded into memory, that string may, at the whim of the OS, be stored in a read-only section of memory. Thus your program fails when it tries to modify the string. --Mike-- http://home.inreach.com/mdunn/ #include "buffy_sig"

          1 Reply Last reply
          0
          • L Lost User

            Thanks, Frank.
            There is another question .
            The program taken from an article "A Sample Generic-Text Program" from MSDN isn't executed in VC++ 6.0 , Whether it modifies a constant string either?
            #include <stdio.h > #include <stdlib.h > #include <string.h > #include <direct.h > #include <errno.h > #include <tchar.h > int __cdecl _tmain(int argc, _TCHAR **argv, _TCHAR **envp) { _TCHAR buff[_MAX_PATH]; _TCHAR *str = _T("Astring"); char *amsg = "Reversed"; wchar_t *wmsg = L"Is"; #ifdef _UNICODE printf("Unicode version\n"); #else /* _UNICODE */ #ifdef _MBCS printf("MBCS version\n"); #else printf("SBCS version\n"); #endif #endif /* _UNICODE */ if (_tgetcwd(buff, _MAX_PATH) == NULL) printf("Can't Get Current Directory - errno=%d\n", errno); else _tprintf(_T("Current Directory is '%s'\n"), buff); _tprintf(_T("'%s' %hs %ls:\n"), str, amsg, wmsg); _tprintf(_T("'%s'\n"), _tcsrev(str)); return 0; }

            F Offline
            F Offline
            Frank Liao
            wrote on last edited by
            #5

            Same Problem. _TCHAR *str = _T("Astring"); is a pointer to a string constant. What you really want is _TCHAR str[] = _T("Astring"); This will store the string into an array. Therefore, it allows _tcsrev (or now _wcsrev) to modify the array. Of course, I can't say that it's "ANSI C" because this is a MS-specific type. Heh, it seems that the sample program is trying to get away with this business. In practice, you should never modify a pointer to a constant string. :) Frank

            1 Reply Last reply
            0
            • L Lost User

              #include <iostream.h> #include <string.h> void main() { char *pstr="test" ; cout<<strrev(pstr)<<endl; } This program isn't executed in VC++6.0, but in C++ Builder5.0, Why?

              R Offline
              R Offline
              Rejeesh
              wrote on last edited by
              #6

              #include #include void main() { char *pstr = new char[5]; strcpy(pstr,"test"); cout<< strrev(pstr) <

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups