Code:int map[NROWS][NCOLS] = {[{-1}][{-1}]};
Code:int map[NROWS][NCOLS] = {[{-1}][{-1}]};
Do you have a question or...?
The line of code does not work. It produces the error message that there is an error making 'map' static.
So how do I initialize the array as -1?
When using memset it errors with ISO C++ forbids declatration of 'memset' with no type.
Last edited by itCbitC; 04-11-2012 at 05:55 PM.
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.
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:
To set them all to -1 using an initializer list you'd have to type out NROWS * NCOLS -1's.Code:int map[NROWS][NCOLS] = {{ 0 }};
So to keep it simple and entirely portable and able to set it to any number you wish, just do this:Code:int a[3][2] = {{-1,-1},{-1,-1},{-1,-1}};
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
Actually, you need nested sets of curly braces, one nesting for each dimension of your array. It should be:
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].Code:int a[3][2] = {{-1, -1}, {-1, -1}, {-1, -1}};
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
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.
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.
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