How could I simple initialize a multidimensional C-array with 0 elements like this:
int a[2][2] = { { 0, 0 }, {0, 0} }
This should work:
int a[2][2] = {0};
EDIT This trick may work for silencing the warning:
int a[2][2] = {{0}};
Use memset:
memset(a,0,sizeof a);
Either use memset(), or make it static and just "lazy-initialize" to zero:
static int a[2][2];
Variables with static storage class are always initialized as if all all their fields were set to 0.
Use bzero to nullify it (faster than memset):
bzero(a, sizeof(a));
Simple: add a semicolon at the end:
int a[2][2] = { { 0, 0 }, {0, 0} };
And, if all the initializers really are zero and the array isn't local, you can omit the initializer altogether because 0 is the default:
int a[2][2];
People are suggesting calling memset or bzero, but those aren't relevant to the question as given.
memset(a, 0, sizeof(a))
Related
I'm new to c, and currently making chess program.
There's the code.
#include <stdio.h>
int spider[8][2] = {{0, 1}, {1, 1}, {1, 0}, {1, -1},
{0, -1}, {-1, -1}, {1, 0}, {-1, 1}};
int jump[8][2] = {{1, 2}, {2, 1}, {2, -1}, {1, -2},
{-2, -1}, {-1, -2}, {-1, 2}, {-2, 1}};
typedef struct
{
int color;
int type;
char symbol;
int unit[8][2];
} piece;
void add_piece(piece piece, int color, int type, char symbol, int unit[8][2])
{
piece.color = color;
piece.type = type;
piece.symbol = symbol;
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 2; j++)
{
piece.unit[i][j] = unit[i][j];
}
}
}
int main(void)
{
piece wk;
add_piece(wk, 0, 0, 'K', spider);
printf("%d", wk.color);
return 0;
}
expected output : 0
console output : 8388608
I found that 8GB = 8388608KB.
What's the meaning of that output?
Is there any problem of initializing property of structure or elsewhere?
Can you see some not good habits in this code?
Remember that in C all arguments are passed by value.
That means the value of the argument in the call is copied into the functions argument value.
So in the call
add_piece(wk, 0, 0, 'K', spider);
the value (current uninitialized contents) of the structure object wk will be copied into the argument variable piece inside the add_piece function.
When you modify the local argument variable, for example through an assignment like
piece.color = color;
you only modify the local copy. The original value used in the call will not be modified.
You can solve this in two ways:
Return the structure, and assign to a variable when doing the call; Or
Emulate pass by reference by using pointers.
Option number 2 is the most common it seems, so I will show it here.
First you need to modify the function to take a pointer to the structure object:
void add_piece(piece *piece, int color, int type, char symbol, int unit[8][2])
{
...
}
Then you need to use the "pointer to structure" syntax using the "arrow" to access structure members:
piece->color = color;
piece->type = type;
// And so on for all access to piece members...
Finally, when you call the function you need to pass a pointer to the wk variable, using the "pointer to" or "address of" operator &:
add_piece(&wk, 0, 0, 'K', spider);
I described the pass by reference emulation because it's so common and because you need to learn to recognize it. For your specific case I would rather recommend that you define a new structure object in the function and return it:
piece add_piece(int color, int type, char symbol, int unit[8][2])
{
piece piece;
piece.color = color;
piece.type = type;
// And so on, much like your current function...
// Return the structure
return piece;
}
Now you can use it as such:
piece wk = add_piece(0, 0, 'K', spider);
Note that sometimes it's not possible, or desirable, to return structures like this, and you still need to emulate pass by reference. So you need to learn both ways.
Lastly a note about the output you get: Uninitialized local (non-static) variables really are uninitialized. They will have indeterminate (read: garbage) values.
Since in your current code doesn't really initialize the original wk structure object, all its members will have such indeterminate values.
Depending on type and use, using such indeterminate values could lead to undefined behavior.
Here is my code snippet
typedef struct Position
{
short X;
short Y;
} Pos;
Pos Block[25*25+1];
void Clear_String (void)
{
memset (&Block, 0, sizeof (Pos));
}
Here is my full code in plain text: Click Me
Why void Clear_String (void) doesn't clear all of the elements in structure?
Block and unfilled are arrays of Pos. You are zeroing only the first item since you pass sizeof (Pos).
You would do better to write:
void Clear_String (void)
{
memset(Block, 0, sizeof(Block));
memset(Unfilled, 0, sizeof(Unfilled));
memset(&Erase, 0, sizeof(Erase));
memset(&Draw, 0, sizeof(Draw));
memset(&Predict, 0, sizeof(Predict));
memset(&Cookie, 0, sizeof(Cookie));
//memset(&Block, 0, sizeof(Block)); // No need to set it twice
memset(&Move, 0, sizeof(Move));
}
Note that you should not pass the address of the array (so just Block instead of &Block). The latter will work, but it isn't what you intended (the type of the variable being passed is not the same, though as it ends up munged to a void *, it isn't obvious). You don't need to zero Block twice. And you should specify the size of the named object, not the size of the type of the named object.
The only time that it requires care is if you are passing arrays by argument, or if the arrays are defined in a separate file from the code the zeroes them. If the declaration is extern SomeType somearray[];, you can't apply sizeof() to it.
I am getting: "error: expected expression before '{' token" for the line I've commented before. If the struct is already defined why would it need a "{" before token. Thanks for any help you can provide.
struct sdram_timing {
u32 wrdtr;
u32 clktr;
};
int calibration(void);
unsigned char read_i2c_cal(void);
static unsigned int eepcal[15];
main() {
DQS_autocalibration();
}
int calibration(void)
{
struct sdram_timing scan_list[30];
read_i2c_cal();
if(eepcal[0] == 0){
scan_list = {{eepcal[1], eepcal[2]}, {-1, -1}}; // <-- PROBLEM LINE
}
else {
//foo
}
return 0;
}
unsigned char read_i2c_cal(void) {
eepcal[0] = 0;
eepcal[1] = 02;
eepcal[2] = 03;
}
The error is because you can't assign an array that way, that only works to initialize it.
int arr[4] = {0}; // this works
int arr2[4];
arr2 = {0};// this doesn't and will cause an error
arr2[0] = 0; // that's OK
memset(arr2, 0, 4*sizeof(int)); // that is too
So applying this to your specific example:
struct sdram_timing scan_list[30];
scan_list[0].wrdtr = 0;
scan_list[0].clktr = 0;
or you could use memset the same way, but instead of sizeof(int) you need size of your structure. That doesn't always work... but given your structure, it will.
Arrays in C language are not assignable. You can't assign anything to the entire array, regardless of what syntax you use. In other words, this
scan_list = { { eepcal[1], eepcal[2] }, {-1, -1} };
is not possible.
In C89/90 you'd have to spell out your assignments line by line
scan_list[0].wrdtr = eepcal[1];
scan_list[0].clktr = eepcal[2];
scan_list[1].wrdtr = -1;
scan_list[1].clktr = -1;
In modern C (post-C99) you can use compound literals to assign entire structs
scan_list[0] = (struct sdram_timing) { eepcal[1], eepcal[2] };
scan_list[1] = (struct sdram_timing) { -1, -1 };
Finally, in modern C you can use memcpy and compound literals to copy data to the array
memcpy(scan_list, (struct sdram_timing[]) { { eepcal[1], eepcal[2] }, {-1, -1} },
2 * sizeof *scan_list);
The last variant, albeit not very elegant, is the closest way to "emulate" array assignment.
You can only use an initializer list in the declaration of the variable, not after the fact.
Initializer list can only be used to initialize an array. You cannot use it afterwards.
However if you use GCC, you can use Compound Literal extension:
scan_list = (struct sdram_timing[30]){{eepcal[1], eepcal[2]}, {-1, -1}};
You might need to change scan_list type to be struct sdram_timing *
I have a problem, I was asked to declare an array of structures, with one structure inside like so:
typedef struct {
int a;
int b;
int c;
}blah;
int main()
{
blah arr[1] = {{0, 0, 0}};
//...
}
Is the above initialization correct?
Yes, it's totally correct.
Array of length 1 is not much different from those containing multiple elements: they all are aggregate types and their initialization should be enclosed in curly braces. If your array had 2 elements, the initialization would be like
blah arr[2] = { {0, 0, 0}, {0, 0, 0} };
Yes.
You don't need to specify the size if you're going to have an explicit initializer, let the compiler figure it out:
blah arr[] = { { 0, 0, 0 } };
I've also included spaces to make the nesting a bit clearer.
Is there a way to declare first and then initialize an array in C?
So far I have been initializing an array like this:
int myArray[SIZE] = {1,2,3,4....};
But I need to do something like this
int myArray[SIZE];
myArray = {1,2,3,4....};
In C99 you can do it using a compound literal in combination with memcpy
memcpy(myarray, (int[]) { 1, 2, 3, 4 }, sizeof myarray);
(assuming that the size of the source and the size of the target is the same).
In C89/90 you can emulate that by declaring an additional "source" array
const int SOURCE[SIZE] = { 1, 2, 3, 4 }; /* maybe `static`? */
int myArray[SIZE];
...
memcpy(myarray, SOURCE, sizeof myarray);
No, you can't set them to arbitrary values in one statement (unless done as part of the declaration).
You can either do it with code, something like:
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 27;
:
myArray[99] = -7;
or (if there's a formula):
for (int i = 0; i < 100; i++) myArray[i] = i + 1;
The other possibility is to keep around some templates that are set at declaration time and use them to initialise your array, something like:
static const int onceArr[] = { 0, 1, 2, 3, 4,..., 99};
static const int twiceArr[] = { 0, 2, 4, 6, 8,...,198};
:
int myArray[7];
:
memcpy (myArray, twiceArr, sizeof (myArray));
This has the advantage of (most likely) being faster and allows you to create smaller arrays than the templates. I've used this method in situations where I have to re-initialise an array fast but to a specific state (if the state were all zeros, I would just use memset).
You can even localise it to an initialisation function:
void initMyArray (int *arr, size_t sz) {
static const int template[] = {2, 3, 5, 7, 11, 13, 17, 19, 21, ..., 9973};
memcpy (arr, template, sz);
}
:
int myArray[100];
initMyArray (myArray, sizeof(myArray));
The static array will (almost certainly) be created at compile time so there will be no run-time cost for that, and the memcpy should be blindingly fast, likely faster than 1,229 assignment statements but very definitely less typing on your part :-).
Is there a way to declare first and
then initialize an array in C?
There is! but not using the method you described.
You can't initialize with a comma separated list, this is only allowed in the declaration. You can however initialize with...
myArray[0] = 1;
myArray[1] = 2;
...
or
for(int i = 1; i <= SIZE; i++)
{
myArray[i-1] = i;
}
This is an addendum to the accepted answer by AndreyT, with Nyan's comment on mismatched array sizes. I disagree with their automatic setting of the fifth element to zero. It should likely be 5 --the number after 1,2,3,4. So I would suggest a wrapper to memcpy() to produce a compile-time error when we try to copy arrays of different sizes:
#define Memcpy(a,b) do { /* copy arrays */ \
ASSERT(sizeof(a) == sizeof(b) && /* a static assert */ \
sizeof(a) != sizeof((a) + 0)); /* no pointers */ \
memcpy((a), (b), sizeof (b)); /* & unnecesary */ \
} while (0) /* no return value */
This macro will generate a compile-time error if your array is of length 1. Which is perhaps a feature.
Because we are using a macro, the C99 compound literal seems to need an extra pair of parentheses:
Memcpy(myarray, ((int[]) { 1, 2, 3, 4 }));
Here ASSERT() is a 'static assert'. If you don't already have your own, I use the following on a number of platforms:
#define CONCAT_TOKENS(a, b) a ## b
#define EXPAND_THEN_CONCAT(a,b) CONCAT_TOKENS(a, b)
#define ASSERT(e) enum {EXPAND_THEN_CONCAT(ASSERT_line_,__LINE__) = 1/!!(e)}
#define ASSERTM(e,m) /* version of ASSERT() with message */ \
enum{EXPAND_THEN_CONCAT(m##_ASSERT_line_,__LINE__)=1/!!(e)}
Why can't you initialize when you declare?
Which C compiler are you using? Does it support C99?
If it does support C99, you can declare the variable where you need it and initialize it when you declare it.
The only excuse I can think of for not doing that would be because you need to declare it but do an early exit before using it, so the initializer would be wasted. However, I suspect that any such code is not as cleanly organized as it should be and could be written so it was not a problem.
The OP left out some crucial information from the question and only put it in a comment to an answer.
I need to initialize after declaring, because will be different
depending on a condition, I mean something like this int
myArray[SIZE]; if(condition1) { myArray{x1, x2, x3, ...} } else
if(condition2) { myArray{y1, y2, y3, ...} } . . and so on...
With this in mind, all of the possible arrays will need to be stored into data somewhere anyhow, so no memcpy is needed (or desired), only a pointer and a 2d array are required.
//static global since some compilers build arrays from instruction data
//... notably not const though so they can later be modified if needed
#define SIZE 8
static int myArrays[2][SIZE] = {{0,1,2,3,4,5,6,7},{7,6,5,4,3,2,1,0}};
static inline int *init_myArray(_Bool conditional){
return myArrays[conditional];
}
// now you can use:
//int *myArray = init_myArray(1 == htons(1)); //any boolean expression
The not-inlined version gives this resulting assembly on x86_64:
init_myArray(bool):
movzx eax, dil
sal rax, 5
add rax, OFFSET FLAT:myArrays
ret
myArrays:
.long 0
.long 1
.long 2
.long 3
.long 4
.long 5
.long 6
.long 7
.long 7
.long 6
.long 5
.long 4
.long 3
.long 2
.long 1
.long 0
For additional conditionals/arrays, just change the 2 in myArrays to the desired number and use similar logic to get a pointer to the right array.
It is not possible to assign values to an array all at once after initialization.
The best alternative would be to use a loop.
for(i=0;i<N;i++)
{
array[i] = i;
}
You can hard code and assign values like --array[0] = 1 and so on.
Memcpy can also be used if you have the data stored in an array already.
There is no such particular way in which you can initialize the array after declaring it once.
There are three options only:
1.) initialize them in different lines :
int array[SIZE];
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
//...
//...
//...
But thats not what you want i guess.
2.) Initialize them using a for or while loop:
for (i = 0; i < MAX ; i++) {
array[i] = i;
}
This is the BEST WAY by the way to achieve your goal.
3.) In case your requirement is to initialize the array in one line itself, you have to define at-least an array with initialization. And then copy it to your destination array, but I think that there is no benefit of doing so, in that case you should define and initialize your array in one line itself.
And can I ask you why specifically you want to do so???