-
January 2nd, 2006, 12:34 PM
#1
Embedded Visual Newbie Help
Hi everyone,
I'm new at C++ and I'm having problems with saving characters to a string pointer at different locations. Here with this example code, I changed the address of the pointer to point to the 5th location that points to a blank, and I want to be able to save the text "5" there. Can anyone help me?
TCHAR *stringadded = TEXT("1234");
TCHAR *a;
int si=0; //string i counter
stringadded++;
si++;
stringadded++;
si++;
stringadded++;
si++;
stringadded++;
si++;
//stringadded=TEXT("5");
a = stringadded - si;
MessageBox(NULL, a, TEXT("HELLO2"), MB_OKCANCEL);
return 0;
-
January 2nd, 2006, 12:39 PM
#2
Re: Embedded Visual Newbie Help
I changed the address of the pointer to point to the 5th location that points to a blank, and I want to be able to save the text "5" there. Can anyone help me?
The 5th location contains an ending '\0'. If you overwrite that '\0' you're not working with an c-string (zero ended) any more, and you're most likely to run into problems.
- petter
-
January 2nd, 2006, 03:08 PM
#3
Re: Embedded Visual Newbie Help
Thank you for your advice. But, actually I need to know how I would add the TEXT("5"); to the string. Can anyone help me again with that?
-
January 2nd, 2006, 03:15 PM
#4
Re: Embedded Visual Newbie Help
i dont know if it would work but if you replace the \0 with a new string it will add a \0 becuse the new string contains that.
Code:
a = TEXT("1234");
a[5] = TEXT("5"); // or (a + 5)
it wont work. its an array of TCHARS (char's and if unicode an array of unsigned short's)
while TEXT("5") is yet again an array of TCHARS, there is no conversion from an array to an non array i think...
-
January 2nd, 2006, 03:33 PM
#5
Re: Embedded Visual Newbie Help
Here is one way to append charaters (or c-strings) to other c-strings.
Code:
// allocate memory for a string (99 charaters + ending zero)
TCHAR* str = new TCHAR[100];
// clear string (set initial character to '\0')
str[0] = _T('\0');
// append data to string
_tcscat(str, _T("some text"));
// print string
_tprintf(_T("%s\n"), str);
// append some more data
_tcscat(str, _T(", some more text"));
// print string
_tprintf(_T("%s\n"), str);
// delete string
delete[] str;
- petter
-
January 2nd, 2006, 03:57 PM
#6
Re: Embedded Visual Newbie Help
Originally Posted by peter_x9
I'm new at C++ and I'm having problems with saving characters to a string pointer at different locations. Here with this example code, I changed the address of the pointer to point to the 5th location that points to a blank, and I want to be able to save the text "5" there. Can anyone help me?
TCHAR *stringadded = TEXT("1234");
TCHAR *a;
First of all you have to understand the difference between pointer which points to literal string and pointer which points to array of chars.
The fistr case present us with the string which cannot be changed at runtime - in fact such attempt produces access violation exception (at least with VC++7, but unfortunately that dumb VC++6 does not ), since strigng literals are allocated in read-only section.
But the second case gives us exactly what you want:
Code:
#include <windows.h>
int main()
{
TCHAR str[10] = TEXT("1234");
TCHAR *a = str;
a[4] = TEXT('5'); // expand the string
a[5] = TEXT('\0'); // end it with zero
MessageBox(NULL, str, TEXT("Hello2"), MB_OKCANCEL);
return 0;
}
Here we have an array initialized with the string - initialization makes the exact string copy at new location, and that new string can be expanded within array.
Last edited by Igor Vartanov; January 2nd, 2006 at 04:01 PM.
Best regards,
Igor
-
January 2nd, 2006, 05:50 PM
#7
Re: Embedded Visual Newbie Help
Thank you so much everyone. You have all been so so much help.
Thank you,
Peter
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|