简体   繁体   中英

Get character array address then convert to pointer char

I convert my string and bytearray to char array.

char nameData[90];
char passData[90];
strcpy(nameData, name.toStdString().c_str()); //string
strcpy(passData, pass.data()); //bytearray

Now I need to get the address of the nameData and passData to set it as a pointer? Or is there an easy to convert the string and bytearray to char pointer?

I need to convert it to char pointer to store it to a file.

I think just use the type conversion would be ok. eg char* pNameData = (char*) nameData . In fact you can use the nameData directly as char* pointer in C or C++.

I need to get the address of the nameData

A char pointer points to a char. It doesn't point to an array strictly speaking.

Your array is an array of char objects however. So, I take it that you actually mean "I need to get the address of the first character of nameData" .

It's quite simple. An array is implicitly convertible to a pointer to the first element of the array. In fact, the array name decays to this pointer whenever the value is used. So, simply assign the pointer:

char* pointer = nameData;

I need to convert it to char pointer to store it to a file.

You probably don't even need the pointer variable. Simply pass the array directly to the function that takes the character pointer argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM