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 initialize const char array?

how to initialize const char array?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialdata-structureshelpquestion
9 Posts 3 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.
  • J Offline
    J Offline
    Joe Smith IX
    wrote on last edited by
    #1

    Hi, The following is the example I have (and works):

    const char *labels[] = {"Mon", "Tue", "Wed"}
    SomeFunctions(labels, 3);

    Now I want to initialize the contents of the array at run-time. I tried the following but an error comes up:

    const char *labels= NULL;
    *labels = new char[3];
    labels[0]=_T("Mon");
    labels[1]=_T("Tue");
    labels[2]=_T("Wed");
    SomeFunctions(labels, 3);

    error C2665: SomeFunctions : none of the 3 overloads can convert parameter 1 from type 'const char *'

    Can someone please show me how declare the same type of array as the examples? Thanks.

    C T 2 Replies Last reply
    0
    • J Joe Smith IX

      Hi, The following is the example I have (and works):

      const char *labels[] = {"Mon", "Tue", "Wed"}
      SomeFunctions(labels, 3);

      Now I want to initialize the contents of the array at run-time. I tried the following but an error comes up:

      const char *labels= NULL;
      *labels = new char[3];
      labels[0]=_T("Mon");
      labels[1]=_T("Tue");
      labels[2]=_T("Wed");
      SomeFunctions(labels, 3);

      error C2665: SomeFunctions : none of the 3 overloads can convert parameter 1 from type 'const char *'

      Can someone please show me how declare the same type of array as the examples? Thanks.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Joe Smith IX wrote:

      *labels = new char[3];

      This is wrong. labels = new char *[3]; looks better. You want to create three char *s, not an array of three chars

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      J 1 Reply Last reply
      0
      • J Joe Smith IX

        Hi, The following is the example I have (and works):

        const char *labels[] = {"Mon", "Tue", "Wed"}
        SomeFunctions(labels, 3);

        Now I want to initialize the contents of the array at run-time. I tried the following but an error comes up:

        const char *labels= NULL;
        *labels = new char[3];
        labels[0]=_T("Mon");
        labels[1]=_T("Tue");
        labels[2]=_T("Wed");
        SomeFunctions(labels, 3);

        error C2665: SomeFunctions : none of the 3 overloads can convert parameter 1 from type 'const char *'

        Can someone please show me how declare the same type of array as the examples? Thanks.

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        can you please show us how is declared SomeFunction(), and what you're trying to do exactly... just a guess: why aren't you using std::vector<> ? -- modified at 4:39 Wednesday 2nd May, 2007 [edit] what about overloading SomeFunction() into Somefunction(const std::vector<std::string>&) and use this code instead :

        std::vectorstd::string vec;
        vec.push_back("mon");
        vec.push_back("tue");
        vec.push_back("wed");

        SomeFunction(vec);

        [/edit]


        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

        J 1 Reply Last reply
        0
        • C Christian Graus

          Joe Smith IX wrote:

          *labels = new char[3];

          This is wrong. labels = new char *[3]; looks better. You want to create three char *s, not an array of three chars

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          J Offline
          J Offline
          Joe Smith IX
          wrote on last edited by
          #4

          oops, you are right. But now that I changed it as you suggested, it added a new error: error C2440: '=' : cannot convert from 'char ** ' to 'const char *'

          C 1 Reply Last reply
          0
          • T toxcct

            can you please show us how is declared SomeFunction(), and what you're trying to do exactly... just a guess: why aren't you using std::vector<> ? -- modified at 4:39 Wednesday 2nd May, 2007 [edit] what about overloading SomeFunction() into Somefunction(const std::vector<std::string>&) and use this code instead :

            std::vectorstd::string vec;
            vec.push_back("mon");
            vec.push_back("tue");
            vec.push_back("wed");

            SomeFunction(vec);

            [/edit]


            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            J Offline
            J Offline
            Joe Smith IX
            wrote on last edited by
            #5

            This function is from a lib. here is the declaration in the header file (Somefunction=StringArray) class StringArray { public : int len; const char * const *data; StringArray() : len(0), data(0) {} StringArray(const char * const *data, int len) : len(len), data(data) {} const char *operator[](int i) const { return data[i]; } };

            T 1 Reply Last reply
            0
            • J Joe Smith IX

              This function is from a lib. here is the declaration in the header file (Somefunction=StringArray) class StringArray { public : int len; const char * const *data; StringArray() : len(0), data(0) {} StringArray(const char * const *data, int len) : len(len), data(data) {} const char *operator[](int i) const { return data[i]; } };

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              Joe Smith IX wrote:

              StringArray(**const char * const ***data, int len)

              oh my god !!! does this really compile, and even if so, is this really necessary, rather that const char* ?? and is this class yours ? why using C style stuff in a C++ code ? (i mean, char* strings when you obviously use classes, so, could use std::string...)


              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

              J 1 Reply Last reply
              0
              • T toxcct

                Joe Smith IX wrote:

                StringArray(**const char * const ***data, int len)

                oh my god !!! does this really compile, and even if so, is this really necessary, rather that const char* ?? and is this class yours ? why using C style stuff in a C++ code ? (i mean, char* strings when you obviously use classes, so, could use std::string...)


                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                J Offline
                J Offline
                Joe Smith IX
                wrote on last edited by
                #7

                like i wrote before, it's not my class. it's part of a lib i am using. and i am bit confused myself (since I always use the easier path - MFC :) so, is there anyway to get around this?

                1 Reply Last reply
                0
                • J Joe Smith IX

                  oops, you are right. But now that I changed it as you suggested, it added a new error: error C2440: '=' : cannot convert from 'char ** ' to 'const char *'

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  const char *labels= NULL;*labels = new char[3];labels[0]=_T("Mon");labels[1]=_T("Tue");labels[2]=_T("Wed");SomeFunctions(labels, 3); Something like char ** labels = new char *[3]; labels[0] = "test"; label[1] = "Not sure if this is exactly right, you may need to use new and strcpy"; label[2] = "I agree with the other poster, you should use vector if you can"; In fact, a vector is a dynamic array, so if you use char * instead of string, the index of the first char in the first item, would represent the start of a block of memory with three strings in it.

                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                  J 1 Reply Last reply
                  0
                  • C Christian Graus

                    const char *labels= NULL;*labels = new char[3];labels[0]=_T("Mon");labels[1]=_T("Tue");labels[2]=_T("Wed");SomeFunctions(labels, 3); Something like char ** labels = new char *[3]; labels[0] = "test"; label[1] = "Not sure if this is exactly right, you may need to use new and strcpy"; label[2] = "I agree with the other poster, you should use vector if you can"; In fact, a vector is a dynamic array, so if you use char * instead of string, the index of the first char in the first item, would represent the start of a block of memory with three strings in it.

                    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                    J Offline
                    J Offline
                    Joe Smith IX
                    wrote on last edited by
                    #9

                    Yup, you nailed it exactly. Thanks a lot. P.S.: Since this is not my class, I cannot change it.

                    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