How to insert char* to char array - WITHOUT using string
-
char *pinNumber = this->gpionum;
cout << " pinNumber " << pinNumber << endl; //TOKThis is what I need - insert char * into char array char constant + char * + char constant I CANNOT USE STRING char *setdir_str ="/sys/class/gpio/gpio"; // + "pinNumber" + "/direction"; the final setdir_str = "/sys/class/gpio/gpioX/direction" I know how the size the setdir_str and can allocate memory for that. I have no idea how to concatenate all individual parts of the array WITHOUT using string. Just char ! Any constructive help would be appreciated, just remember NO STRING!
You have to either use std:string or dynamically allocate/reallocate character arrays to add the needed substrings into.
-
char *pinNumber = this->gpionum;
cout << " pinNumber " << pinNumber << endl; //TOKThis is what I need - insert char * into char array char constant + char * + char constant I CANNOT USE STRING char *setdir_str ="/sys/class/gpio/gpio"; // + "pinNumber" + "/direction"; the final setdir_str = "/sys/class/gpio/gpioX/direction" I know how the size the setdir_str and can allocate memory for that. I have no idea how to concatenate all individual parts of the array WITHOUT using string. Just char ! Any constructive help would be appreciated, just remember NO STRING!
Just use the standard c string functions like strcpy, strcat or even sprintf to do what you want Technically you are trying to concat c strings but also looks like you want a int to ascii conversion. Just messing around here to show sorts of things you can do.
#include
int gpionum = 1;
char buf[256] = { 0 };
sprintf_s(&buf[0], _countof(buf), "/sys/class/gpio/gpio%d/direction", gpionum);
size_t ss = strlen(&buf[0]) + 1;
char* setdir_str = (char*)malloc(ss);
strcpy_s(setdir_str, ss, &buf[0]);setdir_str = "/sys/class/gpio/gpio1/direction" in above example It's all the normal string functions that existed before the string object in c++ existed. There is also the unicode equivalent of each function if you need them in
In vino veritas
-
char *pinNumber = this->gpionum;
cout << " pinNumber " << pinNumber << endl; //TOKThis is what I need - insert char * into char array char constant + char * + char constant I CANNOT USE STRING char *setdir_str ="/sys/class/gpio/gpio"; // + "pinNumber" + "/direction"; the final setdir_str = "/sys/class/gpio/gpioX/direction" I know how the size the setdir_str and can allocate memory for that. I have no idea how to concatenate all individual parts of the array WITHOUT using string. Just char ! Any constructive help would be appreciated, just remember NO STRING!
Vaclav_ wrote:
Any constructive help would be appreciated,
First since you are using C++ you should be using 'new' Steps 1. Call the existing character array A. Call the data to insert S. 2. Determine the size of the NEW array that you need. Add A size plus size of S. 3. Use 'new' to create brand new character array using size from #1. Call this new array X. 4. Determine where you are going to insert the new data. 5. Copy from A into X up to the point where you want to insert 6. Copy all of S into X at the point where you STOPPED in #4 7. Copy the REMAINDER of A (what was left over in A from #4) into X at the point where you STOPPED in #6. Result is now in X.
-
char *pinNumber = this->gpionum;
cout << " pinNumber " << pinNumber << endl; //TOKThis is what I need - insert char * into char array char constant + char * + char constant I CANNOT USE STRING char *setdir_str ="/sys/class/gpio/gpio"; // + "pinNumber" + "/direction"; the final setdir_str = "/sys/class/gpio/gpioX/direction" I know how the size the setdir_str and can allocate memory for that. I have no idea how to concatenate all individual parts of the array WITHOUT using string. Just char ! Any constructive help would be appreciated, just remember NO STRING!
-
Just use the standard c string functions like strcpy, strcat or even sprintf to do what you want Technically you are trying to concat c strings but also looks like you want a int to ascii conversion. Just messing around here to show sorts of things you can do.
#include
int gpionum = 1;
char buf[256] = { 0 };
sprintf_s(&buf[0], _countof(buf), "/sys/class/gpio/gpio%d/direction", gpionum);
size_t ss = strlen(&buf[0]) + 1;
char* setdir_str = (char*)malloc(ss);
strcpy_s(setdir_str, ss, &buf[0]);setdir_str = "/sys/class/gpio/gpio1/direction" in above example It's all the normal string functions that existed before the string object in c++ existed. There is also the unicode equivalent of each function if you need them in
In vino veritas
Thanks Leon. Exactly what I need. Simple and clean... char setdir_str[256]; int n = sprintf(setdir_str, "/sys/class/gpio/gpio%s/direction", this->gpionum); #ifdef CCC_DEBUG printf("[%s] is a string %d chars long\n", setdir_str, n); cout << "direction string " << setdir_str << endl; #endif What is interesting the parameter type passed to sprintf has to be %s "string" - not %c. I sure get mixed up with all these characters / string and string class. Thanks again
-
Thanks Leon. Exactly what I need. Simple and clean... char setdir_str[256]; int n = sprintf(setdir_str, "/sys/class/gpio/gpio%s/direction", this->gpionum); #ifdef CCC_DEBUG printf("[%s] is a string %d chars long\n", setdir_str, n); cout << "direction string " << setdir_str << endl; #endif What is interesting the parameter type passed to sprintf has to be %s "string" - not %c. I sure get mixed up with all these characters / string and string class. Thanks again
Vaclav_ wrote:
What is interesting the parameter type passed to sprintf has to be %s "string" - not %c.
%c is used for a single character. %s - for the char sequence.
-
char *pinNumber = this->gpionum;
cout << " pinNumber " << pinNumber << endl; //TOKThis is what I need - insert char * into char array char constant + char * + char constant I CANNOT USE STRING char *setdir_str ="/sys/class/gpio/gpio"; // + "pinNumber" + "/direction"; the final setdir_str = "/sys/class/gpio/gpioX/direction" I know how the size the setdir_str and can allocate memory for that. I have no idea how to concatenate all individual parts of the array WITHOUT using string. Just char ! Any constructive help would be appreciated, just remember NO STRING!
So have a char array: char* buf="/sys/class/gpio/gpioX/direction" And set the value at 'X' *buf+20 =
-
Vaclav_ wrote:
What is interesting the parameter type passed to sprintf has to be %s "string" - not %c.
%c is used for a single character. %s - for the char sequence.
-
So have a char array: char* buf="/sys/class/gpio/gpioX/direction" And set the value at 'X' *buf+20 =
-
Nice, but than I need to count the pointer where the insertion goes. Which in my case is constant, so not big issue. Thanks for your contribution . Cheers
You can always count backwards from the end, find the first / and insert the value before that. String parsing like this, C style, is done a heck of a lot. It is light weight and very quick.