Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to insert char* to char array - WITHOUT using string

How to insert char* to char array - WITHOUT using string

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancehelptutorial
12 Posts 7 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Vaclav_

    char *pinNumber = this->gpionum;
    cout << " pinNumber " << pinNumber << endl; //TOK

    This 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!

    V Offline
    V Offline
    Victor Nijegorodov
    wrote on last edited by
    #3

    You have to either use std:string or dynamically allocate/reallocate character arrays to add the needed substrings into.

    1 Reply Last reply
    0
    • V Vaclav_

      char *pinNumber = this->gpionum;
      cout << " pinNumber " << pinNumber << endl; //TOK

      This 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!

      L Offline
      L Offline
      leon de boer
      wrote on last edited by
      #4

      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

      V 1 Reply Last reply
      0
      • V Vaclav_

        char *pinNumber = this->gpionum;
        cout << " pinNumber " << pinNumber << endl; //TOK

        This 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!

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #5

        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.

        1 Reply Last reply
        0
        • V Vaclav_

          char *pinNumber = this->gpionum;
          cout << " pinNumber " << pinNumber << endl; //TOK

          This 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!

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          Since this is C++ you should use the string class; that is what it is designed for.

          1 Reply Last reply
          0
          • L leon de boer

            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

            V Offline
            V Offline
            Vaclav_
            wrote on last edited by
            #7

            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

            V 1 Reply Last reply
            0
            • V Vaclav_

              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

              V Offline
              V Offline
              Victor Nijegorodov
              wrote on last edited by
              #8

              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.

              V 1 Reply Last reply
              0
              • V Vaclav_

                char *pinNumber = this->gpionum;
                cout << " pinNumber " << pinNumber << endl; //TOK

                This 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!

                M Offline
                M Offline
                Munchies_Matt
                wrote on last edited by
                #9

                So have a char array: char* buf="/sys/class/gpio/gpioX/direction" And set the value at 'X' *buf+20 =

                V 1 Reply Last reply
                0
                • V Victor Nijegorodov

                  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.

                  V Offline
                  V Offline
                  Vaclav_
                  wrote on last edited by
                  #10

                  Read the details - I am passing SINGLE char. Maybe passing %s includes hidden terminating charterer - hence "sequence ", but the description of the function calls %s a string not a sequence - which makes more sense.

                  1 Reply Last reply
                  0
                  • M Munchies_Matt

                    So have a char array: char* buf="/sys/class/gpio/gpioX/direction" And set the value at 'X' *buf+20 =

                    V Offline
                    V Offline
                    Vaclav_
                    wrote on last edited by
                    #11

                    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

                    M 1 Reply Last reply
                    0
                    • V Vaclav_

                      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

                      M Offline
                      M Offline
                      Munchies_Matt
                      wrote on last edited by
                      #12

                      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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups