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. Array of char*

Array of char*

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structureshelptutorial
7 Posts 5 Posters 0 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.
  • D Offline
    D Offline
    Django_Untaken
    wrote on last edited by
    #1

    Hello all... how do I create array of char*. At the moment I have hardcoded the number of instances (large enough to hold tokens) but this should be generic. Example

    int temp_array[2] = {0};
    char* token1;
    char* token2;

    As you can see, I might not know how many tokens I need at run time. I want something like this

    char* [] tokens;

    How could I fix this? Thanks

    J H M 3 Replies Last reply
    0
    • D Django_Untaken

      Hello all... how do I create array of char*. At the moment I have hardcoded the number of instances (large enough to hold tokens) but this should be generic. Example

      int temp_array[2] = {0};
      char* token1;
      char* token2;

      As you can see, I might not know how many tokens I need at run time. I want something like this

      char* [] tokens;

      How could I fix this? Thanks

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      Create a variable for the array that is initially empty and a variable holding the size:

      char **tokens = NULL;
      unsigned tokenSize = 0;

      Once you know the required number of items, allocate the array. How to do that depends if you are using C or C++:

      tokenSize = items_to_store_in_array;

      // C++
      tokens = new char *[tokenSize];

      /* C */
      tokens = (char **)malloc(sizeof(char*) * tokenSize);

      If you need to change the size use realloc with C. With C++ you should then avoid arrays and use a container like vector - C++ Reference[^] instead.

      D 1 Reply Last reply
      0
      • J Jochen Arndt

        Create a variable for the array that is initially empty and a variable holding the size:

        char **tokens = NULL;
        unsigned tokenSize = 0;

        Once you know the required number of items, allocate the array. How to do that depends if you are using C or C++:

        tokenSize = items_to_store_in_array;

        // C++
        tokens = new char *[tokenSize];

        /* C */
        tokens = (char **)malloc(sizeof(char*) * tokenSize);

        If you need to change the size use realloc with C. With C++ you should then avoid arrays and use a container like vector - C++ Reference[^] instead.

        D Offline
        D Offline
        Django_Untaken
        wrote on last edited by
        #3

        Jochen Arndt wrote:

        How to do that depends if you are using C or C++:

        Well I should have mentioned that earlier..... I am using C. Can you please edit your question (or append the code for C)

        J L 2 Replies Last reply
        0
        • D Django_Untaken

          Jochen Arndt wrote:

          How to do that depends if you are using C or C++:

          Well I should have mentioned that earlier..... I am using C. Can you please edit your question (or append the code for C)

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          The C code is there (the malloc call). Use something like this (for two tokens):

          tokenSize = 2;
          tokens = (char **)malloc(sizeof(char*) * tokenSize);
          tokens[0] = token1;
          tokens[1] = token2;

          /* Use tokens array here */

          /* Release memory and reset variables when not used anymore */
          free(tokens);
          tokens = NULL;
          tokenSize = 0;

          1 Reply Last reply
          0
          • D Django_Untaken

            Hello all... how do I create array of char*. At the moment I have hardcoded the number of instances (large enough to hold tokens) but this should be generic. Example

            int temp_array[2] = {0};
            char* token1;
            char* token2;

            As you can see, I might not know how many tokens I need at run time. I want something like this

            char* [] tokens;

            How could I fix this? Thanks

            H Offline
            H Offline
            Huzifa Terkawi
            wrote on last edited by
            #5

            std::vactor m_myArrayOfString ; m_myArrayOfString.push_back(new char[17]); ... ... in the end loop through vector and delete them one by one for(auto ptr : m_myArrayOfString) delete [] ptr ; m_myArrayOfString.clear();

            huzifa

            1 Reply Last reply
            0
            • D Django_Untaken

              Jochen Arndt wrote:

              How to do that depends if you are using C or C++:

              Well I should have mentioned that earlier..... I am using C. Can you please edit your question (or append the code for C)

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

              Only addition to Jochens answer is to remove confusion to you typedef the char* pointer

              ptrCount = 2;
              typedef char* CHARPTR;
              CHARPTR* charPtrArray = (CHARPTR*)malloc(sizeof(CHARPTR)*ptrcount);

              Now it looks like any other array The confusion exists because char** can be a couple of things in C you can't distinguish them without looking at the initialization code 1.) A pointer to a char* (always true) 2.) An array of char* (if initialized as such) 3.) A multidimensional char array (if initialized as such) Number 3 in particular usually causes mass confusion with students learning C. The typedef doesn't stop you assuming the wrong thing but it does give you language cues to what it is and is easier to read.

              In vino veritas

              1 Reply Last reply
              0
              • D Django_Untaken

                Hello all... how do I create array of char*. At the moment I have hardcoded the number of instances (large enough to hold tokens) but this should be generic. Example

                int temp_array[2] = {0};
                char* token1;
                char* token2;

                As you can see, I might not know how many tokens I need at run time. I want something like this

                char* [] tokens;

                How could I fix this? Thanks

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

                Think of an array of chars as a 2d map. So along the bottom the x axis can be considdered as an array of char pointers, each one pointing to a vertical array of chars going up in the y direction. So point 0,0 is a char** You can of course extend this to a 3 d map, with char*** Any point on the 2d map can be referenced by data[x][y] SO first you alloc how many you need in the x direction: char** data = alloc(xMax) Now each vertical collum can be alloced as data[0] = alloc(y1Max) data[1] = alloc(y2Max) And so on.

                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