Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

how come char array keeps old value when function is recalled

mumpizmumpiz Posts: 75Member
Hi,

I need to feed an existing methode with a pointer to a char array,
but the type Im working with is a CString. So Ive come up with the idea of transforming the string into a char array:

int len = sMagnumName.GetLength();
char szName[ ACWN_KURZSTRING_LAENGE ]; // 128

for( int il = 0; il < len; il++ )
{
szName[il] = sMagnumName[il];
}

which works fine, except for the next time the function is called in a loop. szName still holds the old chars. So if sMagnumName is longer the first time it is passed, than the second time, the first chars in szName are replaced but the other remain.

Example:

sMagnumName first time = "pooltable", second time "bummer" szName is:
b,u,m,m,e,r,b,l,e

Anyone with an answer?
LPCTSTR?

Deleting all chars in a loop when Im done, doesnt seem right?

for( long iC = 0; iC < ACWN_KURZSTRING_LAENGE; iC++ )
{
szName[iC] = NULL;
}

THANKS

Comments

  • tom_swtom_sw Posts: 828Member
    You need to remember to set the terminating NULL character in your string.

    BTW, why are you doing this? CString already has an override that will provide a LPCSTR. If that will not work for you, you can always lock the CString buffer and pass pass the buffer pointer (a char *) into the function directly, then unlock the buffer on return.

    : Hi,
    :
    : I need to feed an existing methode with a pointer to a char array,
    : but the type Im working with is a CString. So Ive come up with the idea of transforming the string into a char array:
    :
    : int len = sMagnumName.GetLength();
    : char szName[ ACWN_KURZSTRING_LAENGE ]; // 128
    :
    : for( int il = 0; il < len; il++ )
    : {
    : szName[il] = sMagnumName[il];
    : }
    [red] szName[il] = '';[/red]
    :
    : which works fine, except for the next time the function is called in a loop. szName still holds the old chars. So if sMagnumName is longer the first time it is passed, than the second time, the first chars in szName are replaced but the other remain.
    :
    : Example:
    :
    : sMagnumName first time = "pooltable", second time "bummer" szName is:
    : b,u,m,m,e,r,b,l,e
    :
    : Anyone with an answer?
    : LPCTSTR?
    :
    : Deleting all chars in a loop when Im done, doesnt seem right?
    :
    : for( long iC = 0; iC < ACWN_KURZSTRING_LAENGE; iC++ )
    : {
    : szName[iC] = NULL;
    : }
    :
    : THANKS
    :
    :

  • mumpizmumpiz Posts: 75Member
    : You need to remember to set the terminating NULL character in your string.
    :
    : BTW, why are you doing this? CString already has an override that will provide a LPCSTR. If that will not work for you, you can always lock the CString buffer and pass pass the buffer pointer (a char *) into the function directly, then unlock the buffer on return.
    :

    Terminating the char with '' character works fine.

    Thanks, Why am I doing this, I didnt know any better and am not familiar with using the LPCSTR operator. Give me a quick example, I cant find anything in the source code available to me

    How about: sprintf( szName, sMagnumName );

    any danger in that?


  • stoberstober Posts: 9,765Member ✭✭✭
    : How about: sprintf( szName, sMagnumName );
    :
    : any danger in that?
    :
    :
    :

    CString and all other MFC objects are fully documented at MSDN
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cstring.asp

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cstring.3a3a.operator_lpctstr.asp

    you can use a CString object in any function that takes char* parameter
    [code]
    // function prototype
    void foo(char*);

    // implementation
    //
    CString str = "Hello World";
    foo((LPCTSTR)str);
    [/code]
Sign In or Register to comment.