Thread: Declare array of array of ints as -1

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    18

    Declare array of array of ints as -1

    Code:
    int map[NROWS][NCOLS] = {[{-1}][{-1}]};

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Do you have a question or...?

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    18
    The line of code does not work. It produces the error message that there is an error making 'map' static.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by spazx View Post
    The line of code does not work. It produces the error message that there is an error making 'map' static.
    That's because [ and ] are not used in initialization but to indicate that the variable is an array.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    18
    So how do I initialize the array as -1?

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by spazx View Post
    So how do I initialize the array as -1?
    Keep it simple?
    Code:
    int map[NROWS][NCOLS];
    memset (&map, -1, sizeof(map));

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    18
    When using memset it errors with ISO C++ forbids declatration of 'memset' with no type.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by spazx View Post
    So how do I initialize the array as -1?
    For example, if you have a 3 x 4 array you could do:
    Code:
    int a[3][2] = {-1,-1,-1,-1,-1,-1};
    The number of initializers should equal the product of "rows x cols".
    This gets cumbersome as the array gets bigger, so stick to memset().
    Last edited by itCbitC; 04-11-2012 at 05:55 PM.

  9. #9
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by spazx View Post
    When using memset it errors with ISO C++ forbids declatration of 'memset' with no type.
    Did you add "#include <string.h>" (without the quotations) at the top of the file?

    Are you compiling in a .cpp file with a C++ compiler? If so, then I think you should be asking about this in the C++ section and not in the C section. And, if so, you should probably add "#include <cstring>" (without quotations) at the top of the file instead.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Using memset as shown above makes assumptions about the representation of ints. And you wouldn't be able to set all elements to -2, or 1, etc., just 0 or -1, and only 0 is completely portable (for ints at least).

    Abbreviating an initializer list sets all unassigned elements to 0, not to copies of some other number you've entered. In particular, to set them all to zero:
    Code:
        int map[NROWS][NCOLS] = {{ 0 }};
    To set them all to -1 using an initializer list you'd have to type out NROWS * NCOLS -1's.
    Code:
        int a[3][2] = {{-1,-1},{-1,-1},{-1,-1}};
    So to keep it simple and entirely portable and able to set it to any number you wish, just do this:
    Code:
        for (r = 0; r < NROWS; r++)
            for (c = 0; c < NCOLS; c++)
                map[r][c] = -1;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by itCbitC View Post
    For example, if you have a 3 x 4 array you could do:
    Code:
    int a[3][2] = {-1,-1,-1,-1,-1,-1};
    The number of initializers should equal the product of "rows x cols".
    This gets cumbersome as the array gets bigger, so stick to memset().
    Actually, you need nested sets of curly braces, one nesting for each dimension of your array. It should be:
    Code:
    int a[3][2] = {{-1, -1}, {-1, -1}, {-1, -1}};
    The outer set of curly braces has 3 elements in it, for the 3 elements of a. Each of those elements is an array initializer itself, initializing a[0], a[1] and a[2].

    Quote Originally Posted by memcpy View Post
    Keep it simple?
    Code:
    int map[NROWS][NCOLS];
    memset (&map, -1, sizeof(map));
    This wont work on architectures that aren't 2's compliment (i.e. where -1 is all-bits 1). memset writes individual bytes/chars. For example, a 32-bit system that uses sign&magnitude instead of 2's compliment would treat a signed char with value -1 as 0x81, and a signed int with value -1 as 0x80000001. However, your memset call would make each int 0x81818181. Yes, the memset prototype specifies an int for the set value parameter, but it's converted to an unsigned char before being copied into each byte you specified.

    @spazx:
    Just write a loop to initialize it, that way if you change NROWS or NCOLS, you don't have to change your initializer:
    Code:
    for i from 0 to NROWS-1
        for j from 0 to NCOLS-1
            map[i][j] = -1

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by anduril462 View Post
    Actually, you need nested sets of curly braces, one nesting for each dimension of your array. It should be:
    Code:
    int a[3][2] = {{-1, -1}, {-1, -1}, {-1, -1}};
    It's nice to have the extra curly braces but they aren't required since an array is laid out linearly, though they certainly make the comprehension easier.
    Quote Originally Posted by anduril462 View Post
    This wont work on architectures that aren't 2's compliment (i.e. where -1 is all-bits 1). memset writes individual bytes/chars. For example, a 32-bit system that uses sign&magnitude instead of 2's compliment would treat a signed char with value -1 as 0x81, and a signed int with value -1 as 0x80000001. However, your memset call would make each int 0x81818181. Yes, the memset prototype specifies an int for the set value parameter, but it's converted to an unsigned char before being copied into each byte you specified.
    Though it's true that the representation of -1 differs in each of the 3 notation systems viz., 2's complement, 1's complement, and signed magnitude. However, the latter two aren't used inside a chip due to the specialized circuitry ($$$) required to correctly perform and interpret the results of numerical calculations. For ex., the ambiguous bit pattern of zero "+/-0" in the latter two notations, since zero is neither positive nor negative.

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I think they actually are required, but I admit I'm to lazy to look up the standard.

    In any event, some compilers will complain about the missing braces whether it is allowed by the standard or not.

    *shrug*

    I actually stopped by to say that `memset' wouldn't work with other values so giving the originator a real solution instead is good.

    Now that I've got that out of the way, zero having two different representations isn't, and never has been, a problem if the hardware handles the values logically. See "IEEE754" floating point numbers and enjoy "0.0==-0.0".

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-03-2011, 05:06 PM
  2. array of ints
    By barim in forum C Programming
    Replies: 4
    Last Post: 01-01-2005, 05:21 AM
  3. How to declare this array?
    By ooosawaddee3 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2002, 01:51 PM
  4. INTs for array size
    By Granger9 in forum C Programming
    Replies: 3
    Last Post: 09-13-2002, 10:02 AM
  5. Array of ints
    By Tiger in forum C Programming
    Replies: 8
    Last Post: 10-01-2001, 12:17 PM