Why??
-
#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?
#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
-
#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
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; } -
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; }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"
-
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; }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
-
#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?