syntax problem

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

    syntax problem

    I'am having problem with the following:

    rec1len = strlen(temp1);

    temp1 is a character array, multi dimensional array.
    it has been initialised as char temp1[12][104];
    int rec1len.
    error for this message: C:\Program Files\Microsoft Visual
    Studio\MyProjec ts\Valid\Zenith 124\Zenith.cpp( 162) : error C2664:
    'strlen' : cannot convert parameter 1 from 'char [12][104]' to 'const
    char *'

    can anyone help please.
  • MiniDisc_2k2

    #2
    Re: syntax problem


    "muser" <charlie12345@h otmail.com> wrote in message
    news:f9a2a258.0 307120516.29240 315@posting.goo gle.com...[color=blue]
    > I'am having problem with the following:
    >
    > rec1len = strlen(temp1);
    >
    > temp1 is a character array, multi dimensional array.
    > it has been initialised as char temp1[12][104];
    > int rec1len.
    > error for this message: C:\Program Files\Microsoft Visual
    > Studio\MyProjec ts\Valid\Zenith 124\Zenith.cpp( 162) : error C2664:
    > 'strlen' : cannot convert parameter 1 from 'char [12][104]' to 'const
    > char *'
    >
    > can anyone help please.[/color]

    That's not how strlen() works. strlen() works with char*s, not character
    arrays.

    If you had something like this:

    char temp1[12];

    It would work if you took the address of that:

    rec1len = strlen(&temp1);

    But because you have a multidimensiona l array, you cannot do that. What I'm
    assuming you're doing is having 104 "strings" which are up to 12 characters
    long (or 12 strings up to 104 characters long). Note I say string here
    meaning the C style string, that is, a char*. Perfectly legal in C++ but a
    little bit harder to work with. If you must keep it char[12][104] and just
    wanted to add up all of the string lengths, here's what you'll want to do:

    rec1len = 0;
    for (int x = 0; x < 104; x++)
    rec1len += strlen(&temp1[x]);

    I'm pretty sure that's how it works. I may have the two indexes mixed up,
    but you're only supposed to put one of them down. I think it's the second
    one, and then take the address of the first. Anyone else there please feel
    free to pipe in if I got it wrong.

    BTW: In C++, we have an easier way of dealing with strings, the aptly named
    string class. First off, you'll need to

    #include <string>
    using namespace std; // if you haven't already

    string temp1[104]; // make 104 strings

    And now to add up the lengths, you could

    rec1len = 0;
    for (int x = 0; x < 104; x++)
    rec1len += temp1[x].length();


    Code Not Compiled
    --
    MiniDisc_2k2


    Comment

    • Bob Jacobs

      #3
      Re: syntax problem

      "muser" <charlie12345@h otmail.com> wrote in message
      news:f9a2a258.0 307120516.29240 315@posting.goo gle.com...[color=blue]
      > I'am having problem with the following:
      >
      > rec1len = strlen(temp1);
      >
      > temp1 is a character array,[/color]

      It's an array of arrays in fact.
      [color=blue]
      > multi dimensional array.
      > it has been initialised as char temp1[12][104];
      > int rec1len.
      > error for this message: C:\Program Files\Microsoft Visual
      > Studio\MyProjec ts\Valid\Zenith 124\Zenith.cpp( 162) : error C2664:
      > 'strlen' : cannot convert parameter 1 from 'char [12][104]' to 'const
      > char *'
      >
      > can anyone help please.[/color]

      What is it that you're intending with strlen(temp1)? It's not 100% clear to
      me.

      If you want string length, which string do you want the length of? You have
      an array of strings so identify which one you want, for instance:

      int rec1len = strlen(temp[0]);

      for the first string, or:

      int rec12len = strlen(temp[11]);

      for the last string.



      Comment

      • John Carson

        #4
        Re: syntax problem

        "muser" <charlie12345@h otmail.com> wrote in message
        news:f9a2a258.0 307120516.29240 315@posting.goo gle.com[color=blue]
        > I'am having problem with the following:
        >
        > rec1len = strlen(temp1);
        >
        > temp1 is a character array, multi dimensional array.
        > it has been initialised as char temp1[12][104];
        > int rec1len.
        > error for this message: C:\Program Files\Microsoft Visual
        > Studio\MyProjec ts\Valid\Zenith 124\Zenith.cpp( 162) : error C2664:
        > 'strlen' : cannot convert parameter 1 from 'char [12][104]' to 'const
        > char *'
        >
        > can anyone help please.[/color]

        Not sure what you are trying to do. strlen is not meant to work with
        multi-dimensional arrays. You can call

        strlen(temp1[i])

        to get the length of row i for any i = 0 to 11. But this will only work if
        the row is nul terminated. strlen counts the number of characters before the
        '\0' character is encountered.


        --
        John Carson
        1. To reply to email address, remove donald
        2. Don't reply to email address (post here instead)

        Comment

        • John Harrison

          #5
          Re: syntax problem


          "muser" <charlie12345@h otmail.com> wrote in message
          news:f9a2a258.0 307120516.29240 315@posting.goo gle.com...[color=blue]
          > I'am having problem with the following:
          >
          > rec1len = strlen(temp1);
          >
          > temp1 is a character array, multi dimensional array.
          > it has been initialised as char temp1[12][104];[/color]

          declared as char temp1[12][104];, there's no initialisation going on there.
          [color=blue]
          > int rec1len.
          > error for this message: C:\Program Files\Microsoft Visual
          > Studio\MyProjec ts\Valid\Zenith 124\Zenith.cpp( 162) : error C2664:
          > 'strlen' : cannot convert parameter 1 from 'char [12][104]' to 'const
          > char *'
          >
          > can anyone help please.[/color]

          What did you expect to happen? strlen is for strings, i.e. *one* dimensional
          character arrays. If you explain what you are trying to do someone will be
          able to help.

          john


          Comment

          Working...