Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to initialize an empty integer array in c++

how to put NULL or empty value to an integer array?

struct book{
char name;
char aut;
int det[10];
};
book bk[40];

here i want every book data type to have there det arrays have value zero for every det[] member.

like image 728
nikhil Avatar asked Dec 31 '14 20:12

nikhil


People also ask

How do you initialize an empty array in C?

int num[5] = {1, 1, 1, 1, 1}; This will initialize the num array with value 1 at all index. The array will be initialized to 0 in case we provide empty initializer list or just specify 0 in the initializer list. Designated Initializer: This initializer is used when we want to initialize a range with the same value.

How do you initialize an array in C?

Array Initialization Using a Loop The following syntax uses a “for loop” to initialize the array elements. This is the most common way to initialize an array in C. // declare an array. int my_array[5];


1 Answers

I think you mean how to zero-initialize an integer array.:)

You can define the array like

int det[10] = {};

or

int det[10] = { 0 };

You may use such a declaration in a structure.

If the array is already defined then you can use memset. For example

#include <cstring>

//...

std::memset( det, 0, sizeof( det ) );
like image 147
Vlad from Moscow Avatar answered Oct 09 '22 16:10

Vlad from Moscow