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. Passing a char array to a routine

Passing a char array to a routine

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiondata-structurestutorial
13 Posts 6 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.
  • C Code o mat

    You could either specify the array size to the function as an additional parameter, or you could terminate your array with NULL and in the function iterate until you get a NULL. Btw, shouldn't this:

    Andy202 wrote:

    char* GetOptions(short nStatus, char* szOptions)

    be instead this:

    char* GetOptions(short nStatus, char **szOptions)

    ?

    > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <

    A Offline
    A Offline
    Andy202
    wrote on last edited by
    #3

    Thanks for your suggestion. But are they anyway I can find out the number of elements in an array like this:-

    char *OPTIONS_ENUMS[4] = {"OPT_1","OPT_2","OPT_3","OPT_4"};

    // Would this work

    for(int j = 0; j < 100; j++)
    {
    if(OPTIONS_ENUMS[j] == NULL)
    return j;
    }

    L G A C 4 Replies Last reply
    0
    • A Andy202

      Thanks for your suggestion. But are they anyway I can find out the number of elements in an array like this:-

      char *OPTIONS_ENUMS[4] = {"OPT_1","OPT_2","OPT_3","OPT_4"};

      // Would this work

      for(int j = 0; j < 100; j++)
      {
      if(OPTIONS_ENUMS[j] == NULL)
      return j;
      }

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

      No, this will (probably) fail; you only have 4 elements in your array so whatever exists at the next word OPTIONS_ENUMS[4] is not guaranteed, it may or may not be NULL. It is a simple matter to declare an array like this:

      char *OPTIONS_ENUMS[] = {"OPT_1","OPT_2","OPT_3","OPT_4", NULL};

      and let the compiler work out how big it needs to be.

      Just say 'NO' to evaluated arguments for diadic functions! Ash

      1 Reply Last reply
      0
      • A Andy202

        Thanks for your suggestion. But are they anyway I can find out the number of elements in an array like this:-

        char *OPTIONS_ENUMS[4] = {"OPT_1","OPT_2","OPT_3","OPT_4"};

        // Would this work

        for(int j = 0; j < 100; j++)
        {
        if(OPTIONS_ENUMS[j] == NULL)
        return j;
        }

        G Offline
        G Offline
        Garth J Lancaster
        wrote on last edited by
        #5

        I agree with Richard's answer - but in some cases, it may not make sense to use NULL as a sentinal marking end of the list, sometimes you may end up defining a non NULL sentinal .. for example, in your list, as long as "EOL" or "End-List" we guaranteed never to be part of your data, you could look through the list until "EOL" or "End-List" were found char *OPTIONS_ENUMS[4] = {"OPT_1","OPT_2","OPT_3","OPT_4", "EOL"}; but I'd stick with NULL unless you had a good reason (eg, NULL could be part of the data if you were specifying an empty option/slot) 'g'

        L 1 Reply Last reply
        0
        • A Andy202

          Thanks for your suggestion. But are they anyway I can find out the number of elements in an array like this:-

          char *OPTIONS_ENUMS[4] = {"OPT_1","OPT_2","OPT_3","OPT_4"};

          // Would this work

          for(int j = 0; j < 100; j++)
          {
          if(OPTIONS_ENUMS[j] == NULL)
          return j;
          }

          A Offline
          A Offline
          Alain Rist
          wrote on last edited by
          #6

          No it will crash, but :)

          char *OPTIONS_ENUMS[5] = {"OPT_1","OPT_2","OPT_3","OPT_4"}; // This works
          for(int j = 0; j < 100; j++)
          {
          if(OPTIONS_ENUMS[j] == NULL)
          return j;
          }

          EDIT: As Richard says in next post this may be error prone and is more a joke than a solution. With C++0x (gcc 4.5 only for now) we can use the standard collections with initializer lists like:

          std::vector<LPCSTR> OPTIONS_ENUMS = {"OPT_1","OPT_2","OPT_3","OPT_4"};

          When not available (MS compilers for now) we must stay in C world and use a macro:

          #define ARRAY_SIZE(_Array) (sizeof(_Array) / sizeof(_Array[0])) // This is the answer

          char *OPTIONS_ENUMS[] = {"OPT_1","OPT_2","OPT_3","OPT_4"};
          for (j = 0; j < ARRAY_SIZE(OPTIONS_ENUMS); j++)
          {
          if(OPTIONS_ENUMS[j] == NULL) // will never happen
          return j;

          // do something with OPTIONS\_ENUMS\[j\]
          

          }

          cheers, AR

          When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

          modified on Thursday, October 21, 2010 6:50 PM

          L A 2 Replies Last reply
          0
          • A Alain Rist

            No it will crash, but :)

            char *OPTIONS_ENUMS[5] = {"OPT_1","OPT_2","OPT_3","OPT_4"}; // This works
            for(int j = 0; j < 100; j++)
            {
            if(OPTIONS_ENUMS[j] == NULL)
            return j;
            }

            EDIT: As Richard says in next post this may be error prone and is more a joke than a solution. With C++0x (gcc 4.5 only for now) we can use the standard collections with initializer lists like:

            std::vector<LPCSTR> OPTIONS_ENUMS = {"OPT_1","OPT_2","OPT_3","OPT_4"};

            When not available (MS compilers for now) we must stay in C world and use a macro:

            #define ARRAY_SIZE(_Array) (sizeof(_Array) / sizeof(_Array[0])) // This is the answer

            char *OPTIONS_ENUMS[] = {"OPT_1","OPT_2","OPT_3","OPT_4"};
            for (j = 0; j < ARRAY_SIZE(OPTIONS_ENUMS); j++)
            {
            if(OPTIONS_ENUMS[j] == NULL) // will never happen
            return j;

            // do something with OPTIONS\_ENUMS\[j\]
            

            }

            cheers, AR

            When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

            modified on Thursday, October 21, 2010 6:50 PM

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

            Both yours and Garth's answers have the array size specified in the declaration, which I always feel is prone to errors in the future. If you add or remove an element or elements and forget to amend the size then your program starts misbehaving. Stick with my solution and let the compiler do the work for you.

            Just say 'NO' to evaluated arguments for diadic functions! Ash

            A 1 Reply Last reply
            0
            • G Garth J Lancaster

              I agree with Richard's answer - but in some cases, it may not make sense to use NULL as a sentinal marking end of the list, sometimes you may end up defining a non NULL sentinal .. for example, in your list, as long as "EOL" or "End-List" we guaranteed never to be part of your data, you could look through the list until "EOL" or "End-List" were found char *OPTIONS_ENUMS[4] = {"OPT_1","OPT_2","OPT_3","OPT_4", "EOL"}; but I'd stick with NULL unless you had a good reason (eg, NULL could be part of the data if you were specifying an empty option/slot) 'g'

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

              See my comments to Alain.

              Just say 'NO' to evaluated arguments for diadic functions! Ash

              1 Reply Last reply
              0
              • L Lost User

                Both yours and Garth's answers have the array size specified in the declaration, which I always feel is prone to errors in the future. If you add or remove an element or elements and forget to amend the size then your program starts misbehaving. Stick with my solution and let the compiler do the work for you.

                Just say 'NO' to evaluated arguments for diadic functions! Ash

                A Offline
                A Offline
                Alain Rist
                wrote on last edited by
                #9

                It was more a joke than an answer, I edited it to a real answer. cheers, AR

                When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

                1 Reply Last reply
                0
                • A Andy202

                  I need to pass an array into a procedure and return a string. The problem is the array could be 1, 2, 3 or 4 character strings. I am stuck as how to do it, see typical arrays below.

                  char *OPTIONS_ENUMS[4] = {
                  "OPT_1",
                  "OPT_2",
                  "OPT_3",
                  "OPT_4"};

                  char *OPTIONS2_ENUMS[4] = {
                  "OPT_1",
                  "OPT_4"};

                  char* GetOptions(short nStatus, char* szOptions)
                  {
                  int i, nOptions;
                  // How can I tell how many elements in the array?
                  nOptions = sizeof(ssmStatusOptions); // This will not work
                  for(i=0; i < nOptions; i++)
                  {
                  if(nStatus == 0)
                  {
                  if(strcmp(szOptions[i], "NULL") == 0)
                  {
                  return "NULL";
                  }
                  else if(strcmp(szOptions[i], "ITEM1") == 0)
                  {
                  return "ITEM1";
                  }
                  else if(strcmp(szOptions[i], "NO_DATA") == 0)
                  {
                  return "NO_DATA";
                  }
                  }
                  else if(nStatus == 1)
                  {
                  if(strcmp(szOptions[i], "OK") == 0)
                  {
                  return "OK";
                  }
                  else if(strcmp(szOptions[i], "ITEM2") == 0)
                  {
                  return "ITEM2";
                  }
                  else if(strcmp(szOptions[i], "NO_DATA") == 0)
                  {
                  return "NO_DATA";
                  }
                  }
                  }
                  }

                  How to call the procedure e.g.

                  GetOptions(1, OPTIONS2_ENUMS);

                  where OPTIONS2_ENUMS is

                  char \*OPTIONS\_ENUMS\[4\] = {
                  		"OPT\_1",
                  		"OPT\_2",
                  		"OPT\_3",
                  		"OPT\_4"};
                  

                  I need to know how many elements in the array and how to pass over the array. Its a c code project. Many thanks.

                  A Offline
                  A Offline
                  Aescleal
                  wrote on last edited by
                  #10

                  I might be really lazy but I'd be tempted to do one of two things: - Pass another parameter into the function that says how big the array is - Bundle the pointer into a structure with the number of entries and use that That way you don't have to worry as much about pointers to pointers and other things that can make you go cross eyed. You're also half way to adding an abstract data type for your options handling as well. From reading your code I'm not sure you're sure you know what you want to achive. The way you've written is it's "return the first array member that matches one of these strings." You've also not got a return value for when you can't find anything. It's been a while since I've programmed in C but I'd have thought your compiler would have a whinge about that! Cheers, Ash

                  1 Reply Last reply
                  0
                  • A Andy202

                    Thanks for your suggestion. But are they anyway I can find out the number of elements in an array like this:-

                    char *OPTIONS_ENUMS[4] = {"OPT_1","OPT_2","OPT_3","OPT_4"};

                    // Would this work

                    for(int j = 0; j < 100; j++)
                    {
                    if(OPTIONS_ENUMS[j] == NULL)
                    return j;
                    }

                    C Offline
                    C Offline
                    Code o mat
                    wrote on last edited by
                    #11

                    Sorry for the late answer (went to bed, was night here), i agree with the others, use NULL or some special string (or a special pointer) to specify the end of the array. Btw, you can also do this:

                    char *OPTIONS_ENUMS[] = {"OPT_1","OPT_2","OPT_3","OPT_4", NULL};

                    this way you don't have to specify the array size, your array's size will be determined by the items you enumerated.

                    > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <

                    1 Reply Last reply
                    0
                    • A Alain Rist

                      No it will crash, but :)

                      char *OPTIONS_ENUMS[5] = {"OPT_1","OPT_2","OPT_3","OPT_4"}; // This works
                      for(int j = 0; j < 100; j++)
                      {
                      if(OPTIONS_ENUMS[j] == NULL)
                      return j;
                      }

                      EDIT: As Richard says in next post this may be error prone and is more a joke than a solution. With C++0x (gcc 4.5 only for now) we can use the standard collections with initializer lists like:

                      std::vector<LPCSTR> OPTIONS_ENUMS = {"OPT_1","OPT_2","OPT_3","OPT_4"};

                      When not available (MS compilers for now) we must stay in C world and use a macro:

                      #define ARRAY_SIZE(_Array) (sizeof(_Array) / sizeof(_Array[0])) // This is the answer

                      char *OPTIONS_ENUMS[] = {"OPT_1","OPT_2","OPT_3","OPT_4"};
                      for (j = 0; j < ARRAY_SIZE(OPTIONS_ENUMS); j++)
                      {
                      if(OPTIONS_ENUMS[j] == NULL) // will never happen
                      return j;

                      // do something with OPTIONS\_ENUMS\[j\]
                      

                      }

                      cheers, AR

                      When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

                      modified on Thursday, October 21, 2010 6:50 PM

                      A Offline
                      A Offline
                      Andy202
                      wrote on last edited by
                      #12

                      Thanks I just tested this and it did not work. The size is always 1. Have I implemented it correct?

                      #define ARRAY_SIZE(_Array) (sizeof(_Array) / sizeof(_Array[0])) // This is the answer

                      char *OPTIONS1_ENUMS[4] = {"OPT_1", "OPT_2", "OPT_3", "OPT_4"};
                      char *OPTIONS2_ENUMS[2] = {"OPT_1", "OPT_4"};
                      char *OPTIONS3_ENUMS[1] = {"OPT_1234"};

                      char* GetOptions(short enumValue, char** szOptions)
                      {
                      int i, nOptions;
                      short size;
                      nOptions = 1;
                      size = ARRAY_SIZE(szOptions);

                      printf("Array Size = %d\\n", size);
                      // do something with OPTIONS\_ENUMS\[j\]
                      

                      }

                      // In the Main program call GetOptions() for 3 different sized arrays
                      GetOptions(1, OPTIONS1_ENUMS);
                      GetOptions(2, OPTIONS2_ENUMS);
                      GetOptions(3, OPTIONS3_ENUMS);

                      A 1 Reply Last reply
                      0
                      • A Andy202

                        Thanks I just tested this and it did not work. The size is always 1. Have I implemented it correct?

                        #define ARRAY_SIZE(_Array) (sizeof(_Array) / sizeof(_Array[0])) // This is the answer

                        char *OPTIONS1_ENUMS[4] = {"OPT_1", "OPT_2", "OPT_3", "OPT_4"};
                        char *OPTIONS2_ENUMS[2] = {"OPT_1", "OPT_4"};
                        char *OPTIONS3_ENUMS[1] = {"OPT_1234"};

                        char* GetOptions(short enumValue, char** szOptions)
                        {
                        int i, nOptions;
                        short size;
                        nOptions = 1;
                        size = ARRAY_SIZE(szOptions);

                        printf("Array Size = %d\\n", size);
                        // do something with OPTIONS\_ENUMS\[j\]
                        

                        }

                        // In the Main program call GetOptions() for 3 different sized arrays
                        GetOptions(1, OPTIONS1_ENUMS);
                        GetOptions(2, OPTIONS2_ENUMS);
                        GetOptions(3, OPTIONS3_ENUMS);

                        A Offline
                        A Offline
                        Alain Rist
                        wrote on last edited by
                        #13

                        Andy202 wrote:

                        Thanks I just tested this and it did not work. The size is always 1. Have I implemented it correct?

                        Nope :) Use it as follows:

                        #define ARRAY_SIZE(_Array) (sizeof(_Array) / sizeof(_Array[0])) // This is the answer

                        char *OPTIONS1_ENUMS[] = {"OPT_1", "OPT_2", "OPT_3", "OPT_4"};
                        char *OPTIONS2_ENUMS[] = {"OPT_1", "OPT_4"};
                        char *OPTIONS3_ENUMS[] = {"OPT_1234"};

                        void GetOptions(short enumValue, char** szOptions, int Size)
                        {
                        printf("Array Size = %d\n", Size);
                        }
                        int main()
                        {
                        // In the Main program call GetOptions() for 3 different sized arrays
                        GetOptions(1, OPTIONS1_ENUMS, ARRAY_SIZE(OPTIONS1_ENUMS));
                        GetOptions(2, OPTIONS2_ENUMS, ARRAY_SIZE(OPTIONS2_ENUMS));
                        GetOptions(3, OPTIONS3_ENUMS, ARRAY_SIZE(OPTIONS3_ENUMS));
                        }

                        BTW you should not call your arrays OPTIONSx_ENUMS: - as capitalized names are conventionnally reserved for C macros, and - they are not enums but C arrays of const char*. cheers, AR

                        When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

                        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