Simple question with array of strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rafi Kfir

    Simple question with array of strings

    Hi,

    This may look as a smiple task to most of you, but to me (a beginner
    with C), it drives me crazy.

    All I want is that one function passes a two dimensional array of
    strings to another function.

    example:
    NOTE: the strings can be of different length and the array will be
    initialized many times to differnt values.

    void fun_1()
    {
    char **Arr[3][2]= {'\0'}; //I want to define a 3x2 array of strings
    of
    //different length (max 32 ch)
    //Arr initialization
    // Arr ["ONE", "1"]
    // ["Two", "2"]
    // ["THREE", "3"]

    fun2(Arr);
    }

    void fun_2(char **rcvArr)
    {

    }

    CAn anybody help me with how to define it, how to initialize it and
    hwo to send and receive it as a parameter.

    I suspect that I must define some buffer length. The question is
    should I select fixed length (e.g. 32 ch per string) or a veriable
    length. In the other option I would probably need to use malloc in
    order to allocate memory...

    I would be happy to be advised of the best way to do it. Also, when I
    initialize the array, I preffer that it will take as little text space
    as possible (for example, the best will be to define it all in one
    line!).

    Thanks for helping
    Rafi
  • Tim Love

    #2
    Re: Simple question with array of strings

    rafi.kfir@telra d.co.il (Rafi Kfir) writes:
    [color=blue]
    >Hi,[/color]
    [color=blue]
    >This may look as a smiple task to most of you, but to me (a beginner
    >with C), it drives me crazy.[/color]
    [color=blue]
    >All I want is that one function passes a two dimensional array of
    >strings to another function.
    > ...[/color]
    [color=blue]
    >I would be happy to be advised of the best way to do it.[/color]
    Look up <vector> and <string> in a C++ book - or online.

    Comment

    • Victor Bazarov

      #3
      Re: Simple question with array of strings

      Rafi Kfir wrote:[color=blue]
      > This may look as a smiple task to most of you, but to me (a beginner
      > with C), it drives me crazy.[/color]

      If you need help with C, you should consider comp.lang.c as your NG of
      choice.
      [color=blue]
      > All I want is that one function passes a two dimensional array of
      > strings to another function.
      >
      > example:
      > NOTE: the strings can be of different length and the array will be
      > initialized many times to differnt values.
      >
      > void fun_1()
      > {
      > char **Arr[3][2]= {'\0'}; //I want to define a 3x2 array of strings
      > of
      > //different length (max 32 ch)[/color]

      In C++ one should always consider standard containers for that:

      vector<vector<s tring> > Arr;

      In C you probably will want to declare an array of pointers to char:

      char *Arr[3][2] = { 0 };

      and then somehow allocate the memory. Although, I'd susupect that to
      have an array of 33-char arrays would be easier (since you limited your
      strings to 32 bytes):

      char Arr[3][2][33];
      [color=blue]
      > //Arr initialization
      > // Arr ["ONE", "1"]
      > // ["Two", "2"]
      > // ["THREE", "3"][/color]

      If you declare your array as 3-dimensional, you still should be able to
      initialise it as

      char Arr[3][2][33] =
      { { "ONE", "1" }, { "Two", "2" }, { "Three", "3" } };
      [color=blue]
      >
      > fun2(Arr);
      > }
      >
      > void fun_2(char **rcvArr)[/color]

      Just repeat the declaration of the Arr in your function declaration (but
      pick the right name for it first, 'fun2' or 'fun_2'):

      void funX(char Arr[3][2][33])
      [color=blue]
      > {
      >
      > }
      >
      > CAn anybody help me with how to define it, how to initialize it and
      > hwo to send and receive it as a parameter.[/color]

      How about a book on C? Do you have any?
      [color=blue]
      > I suspect that I must define some buffer length. The question is
      > should I select fixed length (e.g. 32 ch per string) or a veriable
      > length. In the other option I would probably need to use malloc in
      > order to allocate memory...[/color]

      Right.
      [color=blue]
      > I would be happy to be advised of the best way to do it.[/color]

      There is no "best" way to skin the proverbial cat. There is always more
      than one way and each has its advantages and drawbacks.
      [color=blue]
      > Also, when I
      > initialize the array, I preffer that it will take as little text space
      > as possible (for example, the best will be to define it all in one
      > line!).[/color]

      Yes, you could do that too if your array doesn't change. But it's usually
      a maintenance nightmare.

      const char arr_[] = "ONE\01\0Two\02 \0THREE\03\0";
      /* ^0 ^4 ^6 ^10^12 ^18
      const char* const Arr[3][2] =
      {{ arr_, arr_+ 4 }, { arr_+ 6, arr_+ 10 }, { arr_+12, arr_+18 }};

      V

      Comment

      • John Harrison

        #4
        Re: Simple question with array of strings


        "Rafi Kfir" <rafi.kfir@telr ad.co.il> wrote in message
        news:f74d6851.0 411020835.2a09c 462@posting.goo gle.com...[color=blue]
        > Hi,
        >
        > This may look as a smiple task to most of you, but to me (a beginner
        > with C), it drives me crazy.[/color]

        news:comp.lang. c if you really are interested in C and not C++.
        [color=blue]
        >
        > All I want is that one function passes a two dimensional array of
        > strings to another function.[/color]

        It's impossible to pass arrays in C or C++. You have to use pointers
        instead. This is one reason (among many) to use vectors instead of arrays in
        C++.
        [color=blue]
        >
        > example:
        > NOTE: the strings can be of different length and the array will be
        > initialized many times to differnt values.
        >
        > void fun_1()
        > {
        > char **Arr[3][2]= {'\0'}; //I want to define a 3x2 array of strings[/color]

        char* Arr[3][2] = { { "ONE", "1" }, { "TWO", "2" }, { "THREE", "3" } };
        [color=blue]
        > of
        > //different length (max 32 ch)
        > //Arr initialization
        > // Arr ["ONE", "1"]
        > // ["Two", "2"]
        > // ["THREE", "3"]
        >
        > fun2(Arr);
        > }
        >
        > void fun_2(char **rcvArr)[/color]

        void fun_2(char* (*rcvArr)[2])

        Horrible.
        [color=blue]
        > {
        >
        > }
        >
        > CAn anybody help me with how to define it, how to initialize it and
        > hwo to send and receive it as a parameter.
        >
        > I suspect that I must define some buffer length. The question is
        > should I select fixed length (e.g. 32 ch per string) or a veriable
        > length. In the other option I would probably need to use malloc in
        > order to allocate memory...[/color]

        Yes, you are right.
        [color=blue]
        >
        > I would be happy to be advised of the best way to do it. Also, when I
        > initialize the array, I preffer that it will take as little text space
        > as possible (for example, the best will be to define it all in one
        > line!).[/color]

        Decide what you are programming, C or C++. Post to the appropriate
        newsgroup.

        If you are programming C then you have to do something like the rubbish
        above.

        If you are programming C++, then you can use vector and string and forget
        about the rubbish above.

        #include <vector>
        #include <string>
        using namespace std;

        struct StringPair
        {
        StringPair(stri ng f, string s)
        {
        first = f;
        second = s;
        }
        string first;
        string second;
        };

        typedef vector<StringPa ir> StringArray;

        void fun_2(StringArr ay& Arr)
        {
        // do something with Arr
        }

        int main()
        {
        StringArray Arr;
        Arr.push_back(S tringPair("ONE" , "1"));
        Arr.push_back(S tringPair("TWO" , "2"));
        Arr.push_back(S tringPair("THRE E", "3"));
        }

        Now don't you think that a bit easier. Everything is dynamic, everything
        gets freed automatically.

        john


        Comment

        • sirclif

          #5
          Re: Simple question with array of strings

          #include <stdlib.h>
          #include <stdio.h>

          void print(char *arg[])
          {
          int i,j;
          for( i = 0; i < 3; i++)
          {
          for( j = 0; j < 4; j++)
          {
          printf("%c",arg[i][j]);
          }
          printf("\n");
          }
          }

          int main()
          {
          char **ch;
          int i,j;

          ch = (char**)calloc( 3,sizeof(char*) );
          for(i = 0; i < 3; i++)
          {
          ch[i] = (char *)calloc(4,size of(char));
          }
          for(i = 0; i < 3; i++)
          {
          for(j = 0; j < 4; j++)
          {
          ch[i][j] = 'c';
          }
          }
          print(ch);

          return 0;
          }

          Comment

          • sirclif

            #6
            Re: Simple question with array of strings

            #include <stdlib.h>
            #include <stdio.h>

            void print(char *arg[])
            {
            int i,j;
            for( i = 0; i < 3; i++)
            {
            for( j = 0; j < 4; j++)
            {
            printf("%c",arg[i][j]);
            }
            printf("\n");
            }
            }

            int main()
            {
            char **ch;
            int i,j;

            ch = (char**)calloc( 3,sizeof(char*) );
            for(i = 0; i < 3; i++)
            {
            ch[i] = (char *)calloc(4,size of(char));
            }
            for(i = 0; i < 3; i++)
            {
            for(j = 0; j < 4; j++)
            {
            ch[i][j] = 'c';
            }
            }
            print(ch);

            return 0;
            }

            Comment

            • sirclif

              #7
              Re: Simple question with array of strings

              #include <stdlib.h>
              #include <stdio.h>

              void print(char *arg[])
              {
              int i,j;
              for( i = 0; i < 3; i++)
              {
              for( j = 0; j < 4; j++)
              {
              printf("%c",arg[i][j]);
              }
              printf("\n");
              }
              }

              int main()
              {
              char **ch;
              int i,j;

              ch = (char**)calloc( 3,sizeof(char*) );
              for(i = 0; i < 3; i++)
              {
              ch[i] = (char *)calloc(4,size of(char));
              }
              for(i = 0; i < 3; i++)
              {
              for(j = 0; j < 4; j++)
              {
              ch[i][j] = 'c';
              }
              }
              print(ch);

              return 0;
              }



              Comment

              • sirclif

                #8
                Re: Simple question with array of strings

                oops, guess i should have read the bright red instruction at the top of the
                page telling me not to resubmit if my reply doent show up right away. sorry
                about that

                Comment

                • Jon Bell

                  #9
                  Re: Simple question with array of strings

                  In article <f74d6851.04110 20835.2a09c462@ posting.google. com>,
                  Rafi Kfir <rafi.kfir@telr ad.co.il> wrote:[color=blue]
                  >
                  >This may look as a smiple task to most of you, but to me (a beginner
                  >with C), it drives me crazy.
                  >
                  >All I want is that one function passes a two dimensional array of
                  >strings to another function.[/color]

                  Doing this using old-style arrays and char* "strings" is *not* a simple
                  task, even for experienced programmers. C++ has tools that make it much
                  easier, namely the standard 'vector' and 'string' data types.
                  [color=blue]
                  >example:
                  >NOTE: the strings can be of different length and the array will be
                  >initialized many times to differnt values.[/color]

                  No problem. The standard 'string' type automatically resizes itself
                  appropriately.

                  #include <vector>
                  #include <string>

                  using namespace std;
                  [color=blue]
                  >void fun_1()
                  >{
                  > char **Arr[3][2]= {'\0'}; //I want to define a 3x2 array of strings
                  >of
                  > //different length (max 32 ch)
                  > //Arr initialization
                  > // Arr ["ONE", "1"]
                  > // ["Two", "2"]
                  > // ["THREE", "3"][/color]

                  vector<vector<s tring> > Arr (3, vector<string>( 2));
                  Arr[0][0] = "ONE";
                  Arr[0][1] = "1";
                  // etc. for the other elements
                  [color=blue]
                  >
                  > fun2(Arr);
                  >}
                  >
                  >void fun_2(char **rcvArr)[/color]

                  void fun_2 (vector<vector< string> >& rcvArr)
                  // and make it const if you don't plan to modify the data inside the
                  // function[color=blue]
                  >{
                  >
                  >}[/color]

                  --
                  Jon Bell <jtbellm4h@pres by.edu> Presbyterian College
                  Dept. of Physics and Computer Science Clinton, South Carolina USA

                  Comment

                  • JKop

                    #10
                    Re: Simple question with array of strings


                    void Func(char**);
                    //I don't work with pointers to pointers much
                    //so I'd have to look-up the correct way to make
                    //the above argument const-correct.

                    int main()
                    {
                    char* string_array[3][2];

                    string_array[0][0] = "One";
                    string_array[0][1] = "1";
                    string_array[1][0] = "Two";
                    string_array[1][1] = "2";
                    string_array[2][0] = "Three";
                    string_array[2][1] = "3";

                    Func(string_arr ay);
                    }

                    void Func(char** string_array);
                    {

                    }


                    -JKop

                    Comment

                    Working...