simple prob passing a reference of arrays to a func.

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

    simple prob passing a reference of arrays to a func.

    void DoThang (char (&cMovesDone)[9])
    {
    for(int i = 0; i<9; i++)
    cMovesDone[i] = 'k';
    }

    int main()
    {
    char c;
    DoThang (c);
    cout<<c;
    }

    I tried outputting the cMovesDone using cout<<. It's displaying wierd
    characters. what's wrong?
  • Ian Collins

    #2
    Re: simple prob passing a reference of arrays to a func.

    MC felon wrote:
    void DoThang (char (&cMovesDone)[9])
    {
    for(int i = 0; i<9; i++)
    cMovesDone[i] = 'k';
    }
    >
    int main()
    {
    char c;
    DoThang (c);
    cout<<c;
    }
    >
    I tried outputting the cMovesDone using cout<<. It's displaying wierd
    characters. what's wrong?
    The snippet you posted won't compile.

    --
    Ian Collins.

    Comment

    • MC felon

      #3
      Re: simple prob passing a reference of arrays to a func.

      The snippet you posted won't compile.
      >
      --
      Ian Collins.
      Sorry!
      That piece didn't initialize an array of chars.
      it's basically this:

      #include <max.h>
      #include <dpl.h>

      void DoThang (char (&cMovesDone)[9])
      {
      for(int i = 0; i<9; i++)
      cMovesDone[i] = 'k';
      }

      int main()
      {
      dpl_DosGraphixE nable();
      char c[9] = "aaaaaaaa";
      DoThang (c);
      cout<<c;
      }

      max.h and dpl.h are my own headers. They do little things like make a
      dos app look neater by putting up nice little lines and such
      decorations.
      Ignore them. The problem is when i compile the code above (bereft of
      dpl_DosGraphixE nable()), i get an output of kkkkkkkkkr and some really
      wierd symbols like a small smiley and lines. I mean there are already
      9 k's there already, where did the r and the other stuff come from?
      what should i do?

      Comment

      • Triple-DES

        #4
        Re: simple prob passing a reference of arrays to a func.

        On 19 Feb, 11:10, MC felon <paec....@gmail .comwrote:
        The snippet you posted won't compile.
        >
        --
        Ian Collins.
        >
        Sorry!
        That piece didn't initialize an array of chars.
        it's basically this:
        >
        #include <max.h>
        #include <dpl.h>
        >
        void DoThang (char (&cMovesDone)[9])
        {
         for(int i = 0; i<9; i++)
         cMovesDone[i] = 'k';
        >
        }
        >
        int main()
        {
         dpl_DosGraphixE nable();
         char c[9] = "aaaaaaaa";
         DoThang (c);
         cout<<c;
        >
        }
        >
        max.h and dpl.h are my own headers. They do little things like make a
        dos app look neater by putting up nice little lines and such
        decorations.
        Ignore them. The problem is when i compile the code above (bereft of
        dpl_DosGraphixE nable()), i get an output of kkkkkkkkkr and some really
        wierd symbols like a small smiley and lines. I mean there are already
        9 k's there already, where did the r and the other stuff come from?
        what should i do?
        The problem is that you are missing a \0 character at the end of the
        char array. so operator<< will happily keep printing until it reaches
        a \0 byte in memory. (Actually it's undefined behaviour, but this is
        likely what you're seeing).

        An easy fix would be to make the array one character larger and put a
        '\0' there. Also consider using an std::string or std::vector instead
        of an array.

        Comment

        Working...