Re: Passing Two-Dimensional Array as a Function Parameter

Discussion in 'C++' started by Kai-Uwe Bux, Oct 1, 2010.

  1. Kai-Uwe Bux

    Kai-Uwe Bux Guest

    Jason S wrote:

    > I'm working on a program and have encountered a bit of an issue with
    > getting my program to work correctly. When I print the array in main(),
    > the values print out correctly. But when I debug the program to print
    > out their values in the calculatesquares(int[][], int. int) function,
    > they come out as very large integers. I assume they might be memory
    > addresses? Here's the code:

    [...]
    > int calculatesquares(int squares[][5], int r, int c)
    > {
    > int valsAcross[5];
    > int valsDown[5];
    > int valsDiag[2];

    [...]

    Just a quick guess, but that looks like uninitialized memory. Could you try:

    int valsAcross [5] = {0,0,0,0,0};
    ...


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Oct 1, 2010
    #1
    1. Advertising

  2. Pete Becker <> wrote:
    >> int valsAcross [5] = {0,0,0,0,0};

    >
    > Or, if you don't like counting all those zeros,
    >
    > int valsAcross[5] = { 0 };


    I think this will work too:

    int valsAcross[5] = { };
     
    Juha Nieminen, Oct 3, 2010
    #2
    1. Advertising

  3. Kai-Uwe Bux

    Luc Danton Guest

    On 03/10/2010 15:42, Pete Becker wrote:
    > On 2010-10-03 03:31:50 -0400, Juha Nieminen said:
    >
    >> Pete Becker <> wrote:
    >>>> int valsAcross [5] = {0,0,0,0,0};
    >>>
    >>> Or, if you don't like counting all those zeros,
    >>>
    >>> int valsAcross[5] = { 0 };

    >>
    >> I think this will work too:
    >>
    >> int valsAcross[5] = { };

    >
    > Maybe. But mine is much clearer. <g>
    >


    Do you find:

    template<typename T>
    T make()
    {
    return T();
    }

    unclear ?
    What is the result of make<int>() ?

    What about:

    template<typename T>
    T*
    make()
    {
    return new T[10]();
    }

    ?
    What is make<int>()[0] ?

    To me
    int valsAcross[5] = {};
    is just as clear as
    int valsAcross[5] = { 0 };

    Then again I've seen presentations/read things about C++0x and
    value-initialization.
     
    Luc Danton, Oct 3, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    0
    Views:
    454
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,956
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    688
    Vivek
    Aug 21, 2003
  4. Venkat
    Replies:
    4
    Views:
    1,014
    Venkat
    Dec 5, 2003
  5. Luc Danton
    Replies:
    2
    Views:
    741
    Luc Danton
    Oct 4, 2010
Loading...

Share This Page