question about pointers in a 2-D array and strchr

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • runsun
    New Member
    • May 2007
    • 17

    question about pointers in a 2-D array and strchr

    four strings are in a 2-D array; use strchr to find characters a, b, c, respectively;
    search results are to be put in another 2-D arrray ter[ ][ ]:
    results of searching 'a' in the 1st string go to ter[0][0], 'b' to ter[0][1]...
    ... 2nd string to to ter [1][0], 'b' to ter[1][1]...
    ...
    here are the codes:
    ...
    [code=c] int i, k;
    char str[4][6]={"adevf", "dcdhn", "oledc", "brdca"};
    char chr[3]={'a', 'b', 'c'};
    char ter[i][3];

    for (i=0; i<4; i++)
    {
    for (k=0; j<3; k++);
    {
    &ter[i][k] = strchr(str[i], chr[k]);
    }
    }[/code]
    ...
    when compiling, line 10 is "invalid lvalue in assignment".
    when using (ter[i]+k) = strchr(str[i], chr[k]); - same error.

    So I guess the used the wrong pointer. Actually, I was confused.
    What is wrong?
    Last edited by AdrianH; Jun 5 '07, 06:30 PM. Reason: Please use [code=c][/code] tags for improved reability.
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by runsun

    Code:
     char str[4][6]={"adevf", "dcdhn", "oledc", "brdca"}; 
      [B]int i;[/B]
      char chr[3]={'a', 'b', 'c'};
      [B]char ter[i][3]. [/B] .
    
     for (i=0; i<4; i++).   {
    
          for (k=0; j<3; k++);
            {
                 &ter[i][k] = strchr(str[i], chr[k]);
             }
        }
    ...
    when compiling, line 10 is "invalid lvalue in assignment".
    when using (ter[i]+k) = strchr(str[i], chr[k]); - same error.

    So I guess the used the wrong pointer. Actually, I was confused.
    What is wrong?
    Don't u get error because i is not constant?(Bolde d lines in ur code)

    Now to the point:

    Because two dimensinal arrays doesn't really exist this code:

    char ter[i][3] is same as char ter[3*i],so think about this..

    Savage

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      Originally posted by Savage
      Don't u get error because i is not constant?(Bolde d lines in ur code)

      Now to the point:

      Because two dimensinal arrays doesn't really exist this code:

      char ter[i][3] is same as char ter[3*i],so think about this..

      Savage
      This may work on some compilers. The C99 standard allows for it, and some others have it as an extention. However, i is not initialised so a warning should still show up. Also, don't use bold, it doesn't show up very well with this font.

      As for the OP's original question. What do you want line 10 to do?


      Adrian

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Originally posted by AdrianH
        ...don't use bold, it doesn't show up very well


        Adrian

        OK,thanks.

        Savage

        Comment

        • runsun
          New Member
          • May 2007
          • 17

          #5
          Originally posted by AdrianH
          This may work on some compilers. The C99 standard allows for it, and some others have it as an extention. However, i is not initialised so a warning should still show up. Also, don't use bold, it doesn't show up very well with this font.

          As for the OP's original question. What do you want line 10 to do?


          Adrian
          Thanks!
          line 10 gets the results of strchr, putting them in that two dimensional array. I think the problem is the pointer in line 10. But I don't know how to solve it.
          I think i is initialized, see line 1. (is it right?)

          Comment

          • Savage
            Recognized Expert Top Contributor
            • Feb 2007
            • 1759

            #6
            Originally posted by runsun
            Thanks!
            line 10 gets the results of strchr, putting them in that two dimensional array. I think the problem is the pointer in line 10. But I don't know how to solve it.
            I think i is initialized, see line 1. (is it right?)
            No,it is not initialized.It contains random value.Initializ ed variable has a default value not a random,so this:

            int i=0;

            is a example of initialized value.

            Line 10,tells the compiler to put value returned from strchr in adress of ter,which is not corrrect.



            Savage

            Comment

            • AdrianH
              Recognized Expert Top Contributor
              • Feb 2007
              • 1251

              #7
              Perhaps you intended to declare ter as a 2d array of char pointers? Like this: char* ter[i][3];?


              Adrian

              Comment

              • runsun
                New Member
                • May 2007
                • 17

                #8
                With the help and hints I got from here, I figure it out!
                The key is array of pointers needs to be used instead of pointer to array.
                I tested the following code. It works. Note that it is not necessary to initiate int i=0;
                Many thanks to Savage and AdrianH!

                [code=c] int i, k;
                char str[4][6]={"adevf", "dcdhn", "oledc", "brdca"};
                char chr[3]={'a', 'b', 'c'};
                char *ter[4][3] = {NULL};

                for (i=0; i<4; i++)
                {
                for (k=0; j<3; k++);
                {
                ter[i][k] = strchr(str[i], chr[k]);
                }
                }
                ...[/code]
                Last edited by AdrianH; Jun 6 '07, 12:44 PM. Reason: Change to use [code=c][/code] tags to improve readability.

                Comment

                • AdrianH
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1251

                  #9
                  Originally posted by runsun
                  With the help and hints I got from here, I figure it out!
                  The key is array of pointers needs to be used instead of pointer to array.
                  I tested the following code. It works. Note that it is not necessary to initiate int i=0;
                  Many thanks to Savage and AdrianH!

                  [code=c] int i, k;
                  char str[4][6]={"adevf", "dcdhn", "oledc", "brdca"};
                  char chr[3]={'a', 'b', 'c'};
                  char *ter[4][3] = {NULL};
                  for (i=0; i<4; i++)
                  {
                  for (k=0; j<3; k++);
                  {
                  ter[i][k] = strchr(str[i], chr[k]);
                  }
                  }[/code]
                  ...
                  It is only not necessary to initialise i becasue i is initialised on line 6 prior to use. Please use [code=c][/code] or [code=cpp][/code] tags instead of numbering them yourself. It is easier to read.


                  Adrian

                  Comment

                  • Savage
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1759

                    #10
                    Originally posted by AdrianH
                    It is only not necessary to initialise i becasue i is initialised on line 6 prior to use. Please use [code=c][/code] or [code=cpp][/code] tags instead of numbering them yourself. It is easier to read.


                    Adrian
                    Adrian,now look what ur crack has done to u:

                    i is initialized at line 5 not 6.

                    Tick,tick,tick. .

                    U really,really should get of that crack.

                    Savage

                    Comment

                    • AdrianH
                      Recognized Expert Top Contributor
                      • Feb 2007
                      • 1251

                      #11
                      Originally posted by Savage
                      Adrian,now look what ur crack has done to u:

                      i is initialized at line 5 not 6.

                      Tick,tick,tick. .

                      U really,really should get of that crack.

                      Savage
                      It is all that inferior stuff you give me. :D


                      Adrian

                      Comment

                      Working...